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!
답변 (1개)
James Tursa
2015년 5월 27일
편집: James Tursa
2015년 5월 27일
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
Fariya Rahman
2015년 5월 27일
Image Analyst
2015년 5월 27일
How about using surf()?????
Fariya Rahman
2015년 5월 28일
James Tursa
2015년 5월 28일
Post a new Question, showing the code you are using and the problems/errors you are having.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!