Constructing a circular type of matrix

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?

 Accepted Answer

Stephen23
Stephen23 on 17 Mar 2020
Edited: Stephen23 on 17 Mar 2020
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

More Answers (0)

Categories

Asked:

on 17 Mar 2020

Edited:

on 17 Mar 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!