My for loop is replacing all values of my array with the last iteration.

The point of this loop is to create multiple square 4X4 matrices stored in one variable A, for which the 2 values in the matrix change and are extracted from a 3X20 matrix I have previously created. My for loop, below keeps creates a cell array with 60 elements (like I want) but when the loop is done all the values change to the very last. How can I keep this from happening?
if true
% for i=1:a
for j= 1:b
for k=1:(a*b)
A{k}=[0,1,0,0;-1,0,msigmasq(i,j),0;0,0,0,1;1,0,mplus1sigmasq(i,j),0]
A{1}
end
end
end
end

 Akzeptierte Antwort

Dear Satenig, I think third loop is unnecessary and it is the reason for for changing all the values back to last matrix value. So corrected code is below:
count = 1;
for i = 1:a
for j = 1:b
A{count} = [0,1,0,0;-1,0,msigmasq(i,j),0;0,0,0,1;1,0,mplus1sigmasq(i,j),0];
count = count + 1;
end
end
I hope now it should work fine. Good luck!

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 12 Okt. 2013

Kommentiert:

am 12 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by