matrix with different randperm rows

조회 수: 5 (최근 30일)
Jeremy
Jeremy 2014년 9월 17일
댓글: Jeremy 2014년 9월 18일
Hi,
Say I have vector of N integers, 1 through N. Is there a quick way, without loops, to create a matrix where each row is a different randperm of the vector? My N can get large (up to 100).
Thanks

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 9월 17일
편집: Andrei Bobrov 2014년 9월 17일
[~, out] = sort(rand(M,N),2);
  댓글 수: 3
Mikhail
Mikhail 2014년 9월 18일
Apparently, not-))
Jeremy
Jeremy 2014년 9월 18일
Works orders of magnitude faster for large group sizes :)

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

추가 답변 (3개)

Guillaume
Guillaume 2014년 9월 17일
cell2mat(arrayfun(@(dummy) randperm(n), 1:m, 'UniformOutput', false)');
Whether or not that can be said to be without a loop is debatable as arrayfun is effectively looping over the array 1:m

Mikhail
Mikhail 2014년 9월 17일
  댓글 수: 4
Image Analyst
Image Analyst 2014년 9월 17일
In what universe, or decade, is 100 considered large? You actually mean 100, right, like ten times ten, not 100 million or 100 billion or something?
Jeremy
Jeremy 2014년 9월 17일
100 is large when you talk about some types of group coordination. I need to select k individuals from a group of N to interact at different time steps. What I've been doing is generating all combinations and randomly selecting ones to fill an interaction matrix, but that combination matrix gets big fast.

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


Joseph Cheng
Joseph Cheng 2014년 9월 17일
Well there is a randperm() function that will give you a random permuation. How to get it as a MxN matrix without a loop will take some more time to think about it.
  댓글 수: 1
Joseph Cheng
Joseph Cheng 2014년 9월 17일
편집: Joseph Cheng 2014년 9월 17일
ok well it is possible to do it without a loop in 2ish lines. Use the function arrayfun() or cellfun() on an array of N*ones(M,1).

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by