Loop index is changed on a for loop

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 comentarios

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 el 21 de Mayo de 2021
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

Iniciar sesión para comentar.

Respuestas (1)

Geoff Hayes
Geoff Hayes el 21 de Mayo de 2021
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 comentarios

Geoff Hayes
Geoff Hayes el 21 de Mayo de 2021
Maybe you can simplify your code to use report12 array to get the end time for the current iteration. In fact, I think that the starter variable is really only necessary until you have populate the array with at least one row/record.
Andrea Sbaragli
Andrea Sbaragli el 21 de Mayo de 2021
No starter is necessary in order to keep track of the starting time connected to a bunch of Euclidean distances below 2.
The problem occurs when I have consecutive distance greater than 2.
Andrea Sbaragli
Andrea Sbaragli el 21 de Mayo de 2021
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing
default values.
> In tabular/subsasgnParens (line 432)
In tabular/subsasgnBraces (line 156)
In tabular/subsasgn (line 67)
In interactions (line 42)
Geoff Hayes
Geoff Hayes el 21 de Mayo de 2021
So how should the code handle the case where there are consecutive distances greater than two?
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.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 21 de Mayo de 2021

Comentada:

el 21 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by