Randomise vector numbers to maximum variance

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일
0,1,0,1,...,0,1,0,1,(100 times)
0,10,0,10,...,0,10, (20 times)
0,0,0,0,....0 (280 times)
Or is it important how often the 0 is repeated ?
You should define an exact expression of what you want to minimize or maximize.
abaza
abaza 2022년 9월 9일
Thank you fo the comment!
I mean that I have this vector: [1,1,1,1...1,0,0,0,0,...0,10,10,10....10]
And I want to shuffle the numbers so as to minimize the repetition of 1 and 0 and 10.
It is obvious that I would get repetiton of 0 since the 1 and 10 are less than 1.
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일

0 개 추천

A = repmat([10 repmat([repmat(0, 1, 2), 1 repmat(0, 1, 2)], 1, 5)], 1, 20);

댓글 수: 10

abaza
abaza 2022년 9월 9일
Thank you Bruno for your reply!
Could you please make it more general?? It works for this case, but if I had different cases of numbers and sizes?
Essentially you just need put each chunk of same value regularly spaced, so only largest chunk repeat.
I let you make the code.
abaza
abaza 2022년 9월 9일
I see, this is totally empirical...
Not more empirical then what you call "maximum variance", since the true variance estimated from such sequence doesn't change at all with order.
A = repmat([10 repmat([repmat(0, 1, 2), 1 repmat(0, 1, 2)], 1, 5)], 1, 20);
for k=1:10
Ak = A(randperm(length(A)));
var(Ak)
end
ans = 3.7128
ans = 3.7128
ans = 3.7128
ans = 3.7128
ans = 3.7128
ans = 3.7128
ans = 3.7128
ans = 3.7128
ans = 3.7128
ans = 3.7128
You must have different definition of variance than standard statisics variance anyone knows. But as long as you don't define it, any sequence will be empirical.
abaza
abaza 2022년 9월 9일
편집: abaza 2022년 9월 9일
Thank you! Probably variability or maximum randomisation should be more correct!
Torsten
Torsten 2022년 9월 9일
Probably variability or bst randomisation should be more correct!
And what's the meaning of those ? You operate with vague associations, not with precise definitions.
abaza
abaza 2022년 9월 9일
Minimum number of pairs...
Thanks! Or
if D= my_vector
MINIMUM number of:
unique(find(diff(D)==0))
D = repmat([10 repmat([repmat(0, 1, 2), 1 repmat(0, 1, 2)], 1, 5)], 1, 20);
nnz(diff(D)==0)
ans = 280
D = [repmat([0,1],1,100),repmat([0,10],1,20),zeros(1,280)];
nnz(diff(D)==0)
ans = 279
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개)

카테고리

도움말 센터File Exchange에서 Descriptive Statistics and Visualization에 대해 자세히 알아보기

질문:

2022년 9월 9일

댓글:

2022년 9월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by