Creating an array without mesgrid
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to create an array that looks like this (as an example)
X =
| 1 2 3 |
| 1 2 3 |
| 1 2 3 |
Y =
| 1 1 1 |
| 2 2 2 |
| 3 3 3 |
I tried doing a nested for loop inside a while loop, with this method, the Y array works but not the X array, I sort of understand why its not working but I dont know how to fix it.
this is what I got
X = []; Y = []; c=1;
while c<=3;
for i=1:3
for j=1:3
X(i,c)=j;
Y(i,c)=i;
end
end
c=c+1;
end
I understand that the meshgrid command will do this for me with one line of code, but I have to do it with a nested for loop.
Any suggestions?
댓글 수: 0
채택된 답변
Sven
2014년 5월 20일
You're very close:
for i=1:3
for j=1:3
X(i,j)=j;
Y(i,j)=i;
end
end
Note that you're trying to fill a 2d matrix and you've already got 2 for loops... you can do without the while loop.
Is this what you're looking for?
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!