MATLAB Answers

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

조회 수: 18(최근 30일)
SM
SM 2020년 2월 12일
편집: Stephen 2020년 2월 13일
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!

채택된 답변

John D'Errico
John D'Errico 2020년 2월 12일
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
Stephen
Stephen 2020년 2월 13일
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

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by