For loop taking only last column
Show older comments
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 Comments
Mara
on 31 Jan 2021
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.
Davide Martintoni
on 31 Jan 2021
Mara
on 31 Jan 2021
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
on 31 Jan 2021
Mara
on 1 Feb 2021
Sure, glad I could help
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
on 2 Feb 2021
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.
Bjorn Gustavsson
on 2 Feb 2021
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
on 2 Feb 2021
😄
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!