How can a matrix of randperm be generated with variable n*m?

11 visualizaciones (últimos 30 días)
SM
SM el 12 de Feb. de 2020
Editada: Stephen23 el 13 de Feb. de 2020
I want to generate a matrix of n*m size, which looks like
where
n=3
m=4
A=[randperm(m); randperm(m);randperm(m);]
These value may change with problem size. How can i generate this matrix since n and m vary? No loop please!

Respuesta aceptada

John D'Errico
John D'Errico el 12 de Feb. de 2020
SImpler than you think.
M = 4;
N = 3;
[~,A] = sort(rand(M,N),1)
A =
1 1 3
4 2 1
2 3 4
3 4 2
  1 comentario
Stephen23
Stephen23 el 13 de Feb. de 2020
Editada: Stephen23 el 13 de Feb. de 2020
Sorting horizontally gives an equivalent matrix to that shown in the question:
>> M = 4;
>> N = 3;
>> [~,A] = sort(rand(N,M),2)
A =
4 2 3 1
2 1 4 3
4 1 3 2

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Random Number Generation 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