Storing results from a for loop

1 vue (au cours des 30 derniers jours)
maria
maria le 3 Déc 2013
Commenté : maria le 3 Déc 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?

Réponse acceptée

Wayne King
Wayne King le 3 Déc 2013
Modifié(e) : Wayne King le 3 Déc 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
  1 commentaire
maria
maria le 3 Déc 2013
yes it worked, thanks :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by