Storing results from a for loop

1 Ansicht (letzte 30 Tage)
maria
maria am 3 Dez. 2013
Kommentiert: maria am 3 Dez. 2013
hi, i would like to store the data of a calculation done in a for loop similarly to this http://blogs.mathworks.com/videos/2011/03/09/how-to-store-a-series-of-vectors-from-a-for-loop/ .specifically i need to create a row vector for my results as it is shown in the following code: for x=1:4; for y=1:4; f=2*x+y-1; matf(:,x*y)=f; end end
I run the code without matf(:,x*y), by writing f(x,y) so i know that the correct answer should be: matf=2 4 6 8 3 5 7 9 4 6 8 10 5 7 9 11
Instead of that i get: matf=2 4 6 8 0 7 0 9 8 0 0 10 0 0 0 11
I 'm thinking it must be something with the y iretation but i can't figure it out. can you please help me out?

Akzeptierte Antwort

Wayne King
Wayne King am 3 Dez. 2013
Bearbeitet: Wayne King am 3 Dez. 2013
k = 1;
for x=1:4; for y=1:4;
f(k)=2*x+y-1;
k = k+1;
end
end
Now look at f

Weitere Antworten (0)

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by