Loop index is changed on a for loop

조회 수: 5 (최근 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
Geoff Hayes
Geoff Hayes 2021년 5월 21일
Andrea - so which variable is causing the problem? I see that you do
i= i+1;
which is unnecessary as the for loop will increment i for you.
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
Geoff Hayes
Geoff Hayes 2021년 5월 21일
So how should the code handle the case where there are consecutive distances greater than two?
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by