Repeated Permutation of the same numbers

1 回表示 (過去 30 日間)
MiauMiau
MiauMiau 2016 年 12 月 20 日
コメント済み: MiauMiau 2016 年 12 月 21 日
Hi everyone,
Say I have an array a of 3 numbers (or alternatively of 4 or 5 or..):
a = [0.21 0.018 0.34];
I want to create an array b, which only contains these numbers, however in random order, though each of the numbers should appear the same number of times. For instance if b is [30,1], I would want each of the three numbers to appear 10 times. I know the randperm function, but it seems to me that using randperm is not so straightforward for this task?
Many thanks

採用された回答

Adam
Adam 2016 年 12 月 20 日
e.g. for a length 3, b length 30
idxA = repmat( 1:3, [1, 10] ); % Ensure each element appears same number of times
idxB = randperm( 30 );
idxA = idxA( idxB );
That will give you a random array of 10 each of 1s, 2s and 3s, indicating which of the 3 elements of a should be in each element of b. From there the rest is trivial.

その他の回答 (1 件)

John D'Errico
John D'Errico 2016 年 12 月 20 日
Surely this is no harder than creating a new vector, that has the numbers repeated as often as you wish, then randomly permuting it.
  1 件のコメント
MiauMiau
MiauMiau 2016 年 12 月 21 日
Yes..somehow did not come to my mind. Thx though

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by