MATLAB: Creating an Array with nested for loops.

If I have an array say A = 1:4, how would I create a new 1 x 7 array with the following elements and structure with a nested for loop? I'm very confused.
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4

Best Answer

A = 1:4;
m = numel(A);
[ii,jj] = ndgrid(1:2*m-1);
out = A(max(abs(ii-m),abs(jj-m))+1);
or
m = numel(A);
s = zeros(2*m-1);
s(m,m) = 1;
out = A(bwdist(s,'chessboard')+1);
or with for..end loop
m = numel(A);
out = zeros(2*m-1);
for jj = 1:m
out(jj:m*2-jj,jj:m*2-jj) = A(m + 1 -jj);
end
Related Question