Constructing a circular type of matrix

1 visualización (últimos 30 días)
Vatsala Sharma
Vatsala Sharma el 17 de Mzo. de 2020
Editada: Stephen23 el 17 de Mzo. de 2020
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?

Respuesta aceptada

Stephen23
Stephen23 el 17 de Mzo. de 2020
Editada: Stephen23 el 17 de Mzo. de 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

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by