How to save values in vector for multiple loops?

Hi, so I am writing this code and it uses two for loops. I'm writing a simplified version of the code here..
n=2;
m=3;
k=[1; 2];
for i=1:n
for j=1:m
kk=k(i);
l=j*kk;
end
end
I need to save values for both k=1 and k=2; but it ends up saving values for only the last iteration i.e k=2. The result should be a 3x2 matrix but its a 3x1 matrix..
Thanks in advance!

Respuestas (1)

James Tursa
James Tursa el 27 de Mayo de 2015
Editada: James Tursa el 27 de Mayo de 2015
Maybe this if you want a 3x2 result (but l is a really lousy name for a variable):
n=2;
m=3;
k=[1; 2];
l = zeros(m,n);
for i=1:n
for j=1:m
kk=k(i);
l(j,i)=j*kk;
end
end

4 comentarios

Fariya Rahman
Fariya Rahman el 27 de Mayo de 2015
Thanks, I figured it out.. but I'm running into trouble trying to do a surface plot now.. Would you know how I should do that?
Image Analyst
Image Analyst el 27 de Mayo de 2015
How about using surf()?????
Fariya Rahman
Fariya Rahman el 28 de Mayo de 2015
yeah i was using it. But I wanted to use m not equal to n and it wasn't working out...
James Tursa
James Tursa el 28 de Mayo de 2015
Post a new Question, showing the code you are using and the problems/errors you are having.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 27 de Mayo de 2015

Comentada:

el 28 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by