MATLAB: How i create a matrix from a nested loop

matrixnested loops

For this simple loop
for i=1:5
for j=1:5
A=[i j]
end
end
the result i get is 25 arrays ,2×1 each one. But i want them gathered as a matrix of 25×2.How can i simply do it?

Best Answer

If this is what you're looking for
A = zeros(5*5,2);
c = 0;
for i = 1:5
for j = 1:5
c=c+1;
A(c, :) = [i,j];
end
end
You can do this instead
A = [reshape(repmat(1:5,5,1),25,1), repmat(1:5,1,5)'];