Randomise vector numbers to maximum variance

2 ビュー (過去 30 日間)
abaza
abaza 2022 年 9 月 9 日
コメント済み: abaza 2022 年 9 月 9 日
I have a vector that contains numbers 1, 0 and 10. In partcular, I have 100 times the 1, 400 times the 0 and 20 times the 10.
Is there any function or any way to shuffle the numbers to the maximum variance regarding their repetition? Namely, I want to shuffle them in a way to minimise their sequential repeatibility, so a to achive the low number of times that I get 1,1 or 0,0 or 10,10 in my vector?
Thank you so much in advance!
  4 件のコメント
Torsten
Torsten 2022 年 9 月 9 日
編集済み: Torsten 2022 年 9 月 9 日
Yes, it's obvious that 1 and 10 don't need to repeat and that 0 needs to repeat 279 times. That's optimal and the solution I already posted. Or do you think there is a better one ?
With maximum variance you mean
sum(abs(x(i+1)-x(i)),i=1,...,519)
?
abaza
abaza 2022 年 9 月 9 日
I would like to minimise the repetition.... I cannot avoid repetition of some ZEROS since they are more than 1 and 10, but I would like a more automated way (e.g. when you have more numbers...)

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

採用された回答

Bruno Luong
Bruno Luong 2022 年 9 月 9 日
A = repmat([10 repmat([repmat(0, 1, 2), 1 repmat(0, 1, 2)], 1, 5)], 1, 20);
  10 件のコメント
Bruno Luong
Bruno Luong 2022 年 9 月 9 日
This is a method for generic case
v=[0 1 10];
n=[400 100 20];
A = shuffle(v, n)
A = 1×520
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
function A = shuffle(v, n)
[m,i] = maxk(n,2);
mm = min(m);
if mm > 0
n(i) = n(i)-mm;
A = [repmat(v(i), 1, mm), shuffle(v, n)];
else
[mm,j] = max(m);
A = repmat(v(i(j)), 1, mm);
end
end
abaza
abaza 2022 年 9 月 9 日
That is amazing!!! Thank you Bruno!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDescriptive Statistics and Visualization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by