How to use randperm to create a vector with multiples of the same number?

1 回表示 (過去 30 日間)
lauuser1
lauuser1 2016 年 2 月 22 日
回答済み: Guillaume 2016 年 2 月 22 日
Let's say I want to create a shuffled 1x26 vector with the values 2-11 but the value 10 comes up 4 times. So the vector might look something like
A = 2 10 10 3 4 10 6 7 10 8 9 11 5 10 10 10 2 4 3 5 11 7 9 6 10 8
What line of code would do that? I have previously used a similar code
A_before = repmat(1:13 , [1 2]);
A = A_before(randperm(26))
and would like something similar to that but where the range is 2-11 and value 10 appears four times. (So one batch of 13 values will have four 10s, the whole batch of 26 values will have a total of eight 10s)
  2 件のコメント
Jan
Jan 2016 年 2 月 22 日
編集済み: Jan 2016 年 2 月 22 日
What does "not with consecutive numbers" mean? Please define exactly, what you want to achieve. The less the readers have to guess, the better.
lauuser1
lauuser1 2016 年 2 月 22 日
Well I guess I technically am looking for consecutive numbers but what I'm trying to say is instead of the numbers 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 I want it to be 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11

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

採用された回答

Guillaume
Guillaume 2016 年 2 月 22 日
So, why not?
A_before = repmat([2:11, 10, 10, 10] , [1 2]);
A = A_before(randperm(26))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by