Using ismember to get the corresponding elements
Show older comments
Hi, I am using a function to convert the time to seconds based on specific input. 'time_data_observation_entry,real_time_vec,frame_vec are column vetors and frame_rate_played_video = 8. time_data_observation_entry have two columns. After performing the 'ismember' operation, i am not able to preserve the real_time corresponding to time_data_observation_entry. It looses the structure of the data. Any help to solve this will be appreciated
time_data_observation = load('time_data_observation.mat')
frame_rate_played_video = 8;
real_time_vec = load('real_time_vec');
frame_vec = load('frame_vec');
function [real_time] = time_from_videos_to_second_convertor(time_data_observation_entry,frame_rate_played_video,real_time_vec,frame_vec)
for ii = 1:length(time_data_observation_entry)
if isnan(time_data_observation_entry)
continue
end
frame_id = floor(time_data_observation_entry*frame_rate_played_video);
real_time = real_time_vec(ismember(frame_vec,frame_id));
end
end
3 Comments
Guillaume
on 13 Nov 2018
Notice you don't use ii anywhere inside your loop. As a result, all your loop does is redo the exact same calculation, producing the exact same results multiple times. Also, since you're not iterating over the elements of time_data_observation_entry, your if isnan(xx) will only be true if all the elements of time_data_observation_entry are NaN.
I'm not really clear on what you're trying to do. Since time_data_observation_entry is a two column matrix, so will be frame_id. Then you're asking which elements of the vector frame_vec is found anywhere within the matrix and you're getting a vector the same shape as frame_id back, and select the corresponding elements of real_time_vec. Apart from the fact that you're using both columns of time_data_observation_entry it all makes sense, but if that's not what you want, what do you want?
A small example with actual numbers would help.
Hari krishnan
on 13 Nov 2018
Hari krishnan
on 14 Nov 2018
Accepted Answer
More Answers (1)
Guillaume
on 14 Nov 2018
I think I understand what you want, if so, you're using ismember the 'wrong way round':
function real_time = time_from_videos_to_second_convertor(time_data_observation_entry, frame_rate_played_video, real_time_vec,frame_vec)
frame_id = floor(time_data_observation_entry * frame_rate_played_video);
[found, where] = ismember(frame_id, frame_vec);
assert(all(found(:)), 'Some frames were not found in frame_vec');
real_time = real_time_vec(where);
end
Note that I made the above function error if a frame is not found in frame_vec. Otherwise, I'm not sure what you'd want to do. You could set the corresponding entry to NaN or remove the entire row. It's trivial to do either.
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!