MATLAB: Output in ” for loop”

each output.for loopoutput

I have a question . help me resolve the problem.
I wrote this CODE 1.
———CODE 1 ———-
x=zeros(4,2);
for t = 1 : 2 ;
for k = 1 : 2;
x(t,:)=[t,k]
end
end
---------------------------
so ! I get this result 1.
——–result 1——–
x =
1 2
2 2
0 0
0 0
------------------------
but ! I want another result.
it is result 2 .
————–result 2 ———- x =
1 1
1 2
2 1
2 2
----------------------------------
help me .
thank you for your help in advance.

Best Answer

  • This is less efficient than preallocating, but it works:
    x = [];
    for t = 1 : 2
    for k = 1 : 2;
    x=[x; t k];
    end
    end