Using ismember to get the corresponding elements

1 次查看(过去 30 天)
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 个评论
Hari krishnan
Hari krishnan 2018-11-13
Hi, I am selecting the corresponding elements from the real_time_vec, wherever the elements in the frame_vec contains the elements in frame_id. As you said the time_data_observation has two columns, i want real_time vector to be also two columns corresponding to the operation function is performing.
Currently i have two columns in time_data_observation with 24 rows in each column. Whenever i run the function, i get the real_time vector with a single column of 48 rows and all elements are just arranged in ascending order rather than keeping the structure.
Sample data is attached

请先登录,再进行评论。

采纳的回答

Nick
Nick 2018-11-14
编辑:Nick 2018-11-14
Like Guillaume already mentioned you did not perform any indexing inside your loop and are just repeating the entire operation every time. What i assumed you wanted to do is:
function [real_time] = time_from_videos_to_second_convertor(time_data_observation_entry,frame_rate_played_video,real_time_vec,frame_vec)
real_time = nan(size(time_data_observation_entry));
for ii = 1:length(time_data_observation_entry)
if any(isnan(time_data_observation_entry)) % or all instead of any if you only wanna skip if both entries are nan
continue
end
frame_id = floor(time_data_observation_entry(ii,:)*frame_rate_played_video);
real_time(ii,:) = real_time_vec(ismember(frame_vec,frame_id));
end
end
This will return a real_time vector, with the same format as time_data_observation but nans where they the time_data_observations are nans, you can filter them out of the result or inside the function using logical indexing.
  1 个评论
Guillaume
Guillaume 2018-11-18
Never use length on a matrix. In this particular, length means size(time_data_observation_entry, 1). Depending on the height of that matrix, it could return size(time_data_observation_entry, 2).
I've not tried to understand the code fully in that answer. As far as I can tell, it's a slower and more complicated way of obtaining the same result as mine. Notice that my answer doesn't have any loop and only one call to ismember.

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2018-11-14
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by