MATLAB: Using “for” to create loops

forfor loop

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.

Best Answer

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