how to draw a vector with a set of certain values and a constant average?

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)

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

Hi Andrei,
thank you for your answer, but if I want this vector to be in the length of 50?
thanks again
shiri

Sign in to comment.

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.

Tags

Asked:

on 26 Apr 2012

Community Treasure Hunt

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

Start Hunting!