필터 지우기
필터 지우기

Using "for" to create loops

조회 수: 1 (최근 30일)
Taner Cokyasar
Taner Cokyasar 2016년 7월 6일
댓글: Taner Cokyasar 2016년 7월 6일
I am trying to create a loop to have a (ixm)x2 matrix such as:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
My formulation is as follows:
i = size(input,1);
m = max(input(:,5));
for indice = 1:i;
for indice2 = 1:m;
Zassign = (1,[indice, indice2]);
end
end
I am missing something in here. If you may help me, I would really appreciate it.
  댓글 수: 1
Taner Cokyasar
Taner Cokyasar 2016년 7월 6일
편집: Stephen23 2016년 7월 6일
If possible, can I also write a matrix as following:
Z 1 1
Z 1 2
Z 1 3
Z 2 1
Z 2 2
Z 2 3
Z 3 1
Z 3 2
Z 3 3
I am a new MATLAB user. I know MATLAB deals with numbers. However, I just wanted to ask to make sure if that is not possible.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 7월 6일
편집: Stephen23 2016년 7월 6일
Why waste time with a loop? Code vectorization is much more beautiful!
>> [X,Y] = meshgrid(1:3);
>> mat = [X(:),Y(:)]
mat =
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
And if you really want the Z's you can print them, but they cannot be stored in a numeric matrix (unless you store the character code):
>> fprintf(' Z %d %d\n',mat.')
Z 1 1
Z 1 2
Z 1 3
Z 2 1
Z 2 2
Z 2 3
Z 3 1
Z 3 2
Z 3 3
  댓글 수: 7
Stephen23
Stephen23 2016년 7월 6일
편집: Stephen23 2016년 7월 6일
Did you read the meshgrid documentation ? You can use two inputs to meshgrid:
>> [X,Y] = meshgrid(1:10,1:3);
>> mat = [X(:),Y(:)]
mat =
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
5 1
5 2
5 3
6 1
6 2
6 3
7 1
7 2
7 3
8 1
8 2
8 3
9 1
9 2
9 3
10 1
10 2
10 3
Taner Cokyasar
Taner Cokyasar 2016년 7월 6일
After this answer, yes :) I appreciate it.

댓글을 달려면 로그인하십시오.

추가 답변 (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