MATLAB Answers

Loop index is changed on a for loop

3 ビュー (過去 30 日間)
Andrea Sbaragli
Andrea Sbaragli 2021 年 5 月 21 日
コメント済み: Image Analyst 2021 年 5 月 21 日
Dear all, I've been trying to debug this issue but I m not able to solve it.
After having calculated the Euclidean distance between points I have to check whether or not it is below a certain threshold. If yes I save some statistics and I go for the next row. As soon as the Euclidean distance is higher than 2 I save in another table (c- index that must be different than i) these statistics per each coloumn in the row c :
1- date time associated to the first distance below to the threshold
2- date time associated to the last distance below to the threshold
3- date time = difference between 1 and 2
4- Mean distance
Of course, the index c increases by one in a different manner than i. I paste below my code.
function [report12,d12] = interactions(T1,T2, j, k, stamp1, stamp2)
T1 = array2table(T1);
T2 = array2table(T2);
%stamp1 = array2table(stamp1)
stamp2 = array2table(stamp2)
report12 =table;
d12 = array2table(zeros(size(stamp1)));
i = 1;
counter = 0;
n = 0;
c=1;
starter= 0;
start_time = NaT;
end_time = NaT;
duration = NaT;
mean_distance = 0;
stop_contact=0;
% reshape array size
T2(1:k-j,:)=[]; %same dim of T1
stamp2(1:k-j,:)=[];
for i= 1:height(T1)
d12{i,1} = (norm(T1{i,3:end} - T2{i,3:end} ));
if d12{i,1} <= 2.0
starter= starter +1;
counter = counter +d12{i,1};
n= n+1;
if starter == 1
start_time= stamp1(i,1)
end
if starter >1
end_time = stamp1(i,1)
duration = end_time - start_time;
mean_distance = counter/n;
end
end
if (d12{i,1} > 2.0) && (starter ~= 1)
report12{c,1} = start_time;
report12{c,2} = end_time;
report12{c,3} = duration;
report12{c,4} = mean_distance;
c=c+1;
counter = 0;
n=0;
starter=0;
end_time=NaT;
start_time=NaT;
%%duration= NaT;
mean_distance = 0;
%stop_contact = 1;
end
i= i+1;
end
end
  2 件のコメント
Andrea Sbaragli
Andrea Sbaragli 2021 年 5 月 21 日
This is my output table report12:
At this point I cannot understand why some row have null values since I fill the table only under this condition, setting starter again equal to 0:
if (d12{i,1} > 2.0) && (starter ~= 1)
report12{c,1} = start_time;
report12{c,2} = end_time;
report12{c,3} = duration;
report12{c,4} = mean_distance;
c=c+1;
counter = 0;
n=0;
starter=0;
end_time=NaT;
start_time=NaT;
%%duration= NaT;
mean_distance = 0;
%stop_contact = 1;
end

サインインしてコメントする。

回答 (1 件)

Geoff Hayes
Geoff Hayes 2021 年 5 月 21 日
Andrea - when you populate a row in a table, you reset the start and end times
end_time=NaT;
start_time=NaT;
but what guarantee is there on the next iteration of the loop, that the first condition will be satisfied so that you re-initialize these variables
for i= 1:height(T1)
d12{i,1} = (norm(T1{i,3:end} - T2{i,3:end} ));
if d12{i,1} <= 2.0
If the distance is greater than 2 then you will skip this if statement and then go immediately to the other and populate the row with these NaT values. Try confirming this behaviour with the MATAB Debugger.
  5 件のコメント
Image Analyst
Image Analyst 2021 年 5 月 21 日
Andrea, you keep forgetting to attach your data. Make it easy for people to help you, not hard.
Here are the guidelines.
Please attach your variables in a .mat file so we can run your code.
save('answers.mat', 'T1','T2', 'j', 'k', 'stamp1', 'stamp2');
Then attach answers.mat with the paperclip icon.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by