Hi,
I am trying a code that creates two vector columns (36x1) and then store the two columns in a matrix. The problem is that the code is repeating the second column sector when storing it in a vector. I tried several option with the foor loops but the results doesn't change. Matrice is matrix in english if it can help!
for i=1:5:6
%here i start from a big matrix and I take 4 rows for each iteration, all columns
x_kalman_rolling_matrice_for = x_kalman_rolling_matrice(i+2:i+5,1:36);
for i=1:2
%here i take all rows from a matrix, but only the first 36 columns (in second iteration from the 2nd to the 37th column);
regressori_girati_for = regressori_girati(:,i:i+35);
for i=1:36
%here i compute a number given by the transpose of a columns from the x_kalman_rolling_matrice_for, so a row, and I multiply it by a column
fitted_kalman_rolling = transpose(x_kalman_rolling_matrice_for(:,i))*regressori_girati_for(:,i);
fitted_kalman_rolling_matrice(i,1)=fitted_kalman_rolling; %here I put one numnber under the other to create a vector
end
end
for i=1:2
fitted_kalman_rolling_matrice_grande(:,i) = [fitted_kalman_rolling_matrice]; %here I would like the two vector to be written one column after the other
%but printing the results shows me that the code is taking only the second vector
end
end

댓글 수: 10

Mara
Mara 2021년 1월 31일
Hey Davide, if you have nested for-loops, you cannot call the looping variable i in all loops. Otherwise i will be overwritten and your loops won't work the way you expect them to work.
Hi Mara,
thank you for the suggestion. I tried to modify the looping variable in the iteration to this:
for j=1:5:6
x_kalman_rolling_matrice_for = x_kalman_rolling_matrice(j+2:j+5,1:36);
for k=1:2
regressori_girati_for = regressori_girati(:,k:k+35);
for i=1:36
fitted_kalman_rolling = transpose(x_kalman_rolling_matrice_for(:,i))*regressori_girati_for(:,i);
fitted_kalman_rolling_matrice(i,1)=fitted_kalman_rolling;
end
end
fitted_kalman_rolling_matrice_grande(:,j) = [fitted_kalman_rolling_matrice];
end
Now it works, giving two different columns vector. But, shall I always (when doing nested loop) change the looping variable? I used the same "i" variable in previous cases and it worked fine.
Mara
Mara 2021년 1월 31일
Good to hear it works now! Maybe you should recheck these previous loops in detail, whether they behave correctly, because as far as I know you should never use the same looping variable in nested loops.
Have a look at this discussion, too
Best,
Mara
Davide Martintoni
Davide Martintoni 2021년 1월 31일
Yes, I have seen that the values generated by the code change when changing the looping variable. Thanks for the reference to the previous discussion. Really appreciate the help. Thanks
Mara
Mara 2021년 2월 1일
Sure, glad I could help
Adam Danz
Adam Danz 2021년 2월 2일
편집: Adam Danz 2021년 2월 2일
Using smart indentation (ctrl+a, ctrl+i) really helps in identifying these types of problems.
Your code with smart indentation,
for i=1:5:6
%here i start from a big matrix and I take 4 rows for each iteration, all columns
x_kalman_rolling_matrice_for = x_kalman_rolling_matrice(i+2:i+5,1:36);
for i=1:2
%here i take all rows from a matrix, but only the first 36 columns (in second iteration from the 2nd to the 37th column);
regressori_girati_for = regressori_girati(:,i:i+35);
for i=1:36
%here i compute a number given by the transpose of a columns from the x_kalman_rolling_matrice_for, so a row, and I multiply it by a column
fitted_kalman_rolling = transpose(x_kalman_rolling_matrice_for(:,i))*regressori_girati_for(:,i);
fitted_kalman_rolling_matrice(i,1)=fitted_kalman_rolling; %here I put one numnber under the other to create a vector
end
end
for i=1:2
fitted_kalman_rolling_matrice_grande(:,i) = [fitted_kalman_rolling_matrice]; %here I would like the two vector to be written one column after the other
%but printing the results shows me that the code is taking only the second vector
end
end
Bjorn Gustavsson
Bjorn Gustavsson 2021년 2월 2일
Another helpful notation convention is to number the loop-variables i1, i2, i3 etc just to keep track of which loop-variable is which in the "nesting". This has helped me tremendously.
Adam Danz
Adam Danz 2021년 2월 2일
편집: Adam Danz 2021년 2월 2일
That's an interesting approach, Bjorn. I reserve i,j,k for loop variables and rarely have more than 3 nested loops but when I do, I arrive to a mental impasse on coming up with loop var names. I haven't thought of numbering them....
Bjorn Gustavsson
Bjorn Gustavsson 2021년 2월 2일
That was what I always used to (but then my memory went somewhere I didn't follow), but I too often forgot which order I had i, j and k. Now I only rely on remembering the order of the natural numbers, when that goes I figure people will tell me to stop messing up programming?
Adam Danz
Adam Danz 2021년 2월 2일
😄

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2021년 1월 31일

댓글:

2021년 2월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by