Storing results from a for loop

1 次查看(过去 30 天)
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?

采纳的回答

Wayne King
Wayne King 2013-12-3
编辑:Wayne King 2013-12-3
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by