MATLAB: Constructing a circular type of matrix

matricesmatrix manipulation

I want a matrix that is of the form:
Right now I am doing it as follows:
w = rand(M,N)
for i = 1:M
for j = 1:N
u = max(i,j);
w(i,j) = u;
end
end
But I don't like using the double for loops. Is there another way?

Best Answer

For MATLAB versions >=R2016b:
>> M = max((1:4).',1:5)
M =
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
For earlier versions:
>> [X,Y] = ndgrid(1:4,1:5);
>> M = max(X,Y)
M =
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5