MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#" onclick="window._kiq.push(['set', { 'event': 'Turn off MT' }]);">ここ</a>」をクリックしてください。

0

Populating values in a vector based on function

Hari krishnan さんによって質問されました 2018 年 10 月 15 日
最新アクティビティ dpb
さんによって コメントされました 2018 年 10 月 16 日
dpb
さんの 回答が採用されました
I have an excel file as attached. In the second column, i have the 'entry time' and third column have 'exit time'. In the 'exit time', there are few values missing. I am doing an functional operation, which takes the 'entry time' and 'exit time'. When i perform this, i am getting an error "Subscript indices must either be real positive integers or logicals", due to the presence of nan's. Any help to solve this will be appreciated.
real_time_vec = str2double(changed_ts_to_seconds{1,2}(:,5)); %choose good nest data text file
frame_rate_played_video = 8;
colony_data = 'ants_entry_exit_goodnest_14.0164.xlsx';
read_colony_observation_data_goodnest = xlsread(colony_data);
time_data_observation = floor(read_colony_observation_data_goodnest(:,2:3)*60);
entry_times_to_nest = time_from_videos_to_second_convertor(time_data_observation,frame_rate_played_video,real_time_vec);
The function 'time_from_videos_to_second_convertor' is shown below
function [real_time] = time_from_videos_to_second_convertor(time_data_observation,frame_rate_played_video,real_time_vec)
for ii = 1:length(time_data_observation)
frame_id = time_data_observation*frame_rate_played_video;
real_time = real_time_vec(frame_id);
end

  2 件のコメント

dpb
2018 年 10 月 15 日
So, what do you expect for a result when there is the missing value?
Hari krishnan 2018 年 10 月 15 日
I just want to keep nan

サインイン to comment.

タグ

1 件の回答

回答者: dpb
2018 年 10 月 15 日
編集済み: dpb
2018 年 10 月 16 日
 採用された回答

function [real_time] = time_from_videos_to_second_convertor(time_data_observation,frame_rate_played_video,real_time_vec)
for ii = 1:length(time_data_observation)
frame_id = time_data_observation*frame_rate_played_video;
real_time = real_time_vec(frame_id);
end
  1. in loop on ii, time_data_observation is referring to the whole array, not just single element
  2. is it guaranteed that time_data_observation*frame_rate_played_video is integer even if not NaN
  3. real_time is overwritten every pass thru the loop.
If you can't fix the NaN from happening, probably the simplest fix to your current code would be
real_time=nan(size(time_data_observation)); % initialize to NaN for unknown
for ii = 1:length(time_data_observation)
frame_id = time_data_observation(ii)*frame_rate_played_video;
try % trap index error on missing value
real_time(ii) = real_time_vec(frame_id); % we're ok
catch % on error do nothing, already initialized
end
You could write this without loop as "more Matlab-y" solution and "exercise for Student" if wish a challenge! :) end

  2 件のコメント

Hari krishnan 2018 年 10 月 16 日
thank you. I tried it writing without the loop. It worked.
dpb
2018 年 10 月 16 日
+1! :)

サインイン to comment.



Translated by