matrix with different randperm rows

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 日

5 投票

[~, out] = sort(rand(M,N),2);

3 件のコメント

Jeremy
Jeremy 2014 年 9 月 17 日
Thanks. Now let me see if it speeds things up.
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 日

1 投票

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 日

0 投票

4 件のコメント

Jeremy
Jeremy 2014 年 9 月 17 日
編集済み: Jeremy 2014 年 9 月 17 日
Thanks for the quick response, but my N can get large (up to 100) and rows with the same permutation are OK.
Mikhail
Mikhail 2014 年 9 月 17 日
You originally wrote different permutation. Use randperm function for your programm. I don't know how do it without loop.
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 日

0 投票

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).

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

タグ

質問済み:

2014 年 9 月 17 日

コメント済み:

2014 年 9 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by