how to draw a vector with a set of certain values and a constant average?
Show older comments
let say I want to create a vector with the numbers 10, 15, 20 only and with the average of 15 in a length of 4.
I want to get every time a different sequence, randomly.
for example:
[10 20 20 10] or
[15 20 10 15] or
[15 10 15 20] etc.
Thank you
Shiri
Answers (2)
Andrei Bobrov
on 26 Apr 2012
A = nchoosek(reshape(ones(2,1)*[10 15 20],1,[]),4);
M = unique(A(mean(A,2) == 15 & ~all(diff(A,1,2)==0,2),:),'rows');
out = M(randi(2),randperm(4));
1 Comment
Shiri Simon
on 26 Apr 2012
Walter Roberson
on 26 Apr 2012
0 votes
Once you have randomly determined the first three values, the remaining values will be pre-determined by the requirement that each subsequence have an average of 15. Each new entry will be 60 minus sum() of the 3 previous entries. This will always lead to a repeating pattern of length 1, 2, or (most likely) 4.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!