Randomly select values from a vector that yield a target mean
Show older comments
I have a specific list of values, which can be arrayed in a vector: AR = [-.5, -.3, -.1, 0, .1, .3, .5] (note that it is not important for the values to be arranged in a vector necessarily)
I want to specify a target mean. E.g.: TM = -.1
Then, I want ML to randomly select any N values (with replacement) from vector AR, as long as those N values yield the target mean. For example,
AR = [-.5, -.3, -.1, 0, .1, .3, .5] TM = -.1 N = 4
*magic code goes here, and ML gives:
Output = [-.3, .1, -.5, .3]
N randomly selected values from a given set with a mean of TM. Any ideas how to achieve this?
2 Comments
Jos (10584)
on 1 Mar 2018
In general and most cases, this won't be possible (e.g., AR = [1 2 6], TM = 3, N= 2), unless you have some additional constraints on the inputs AR, TM, and N. Do you know these constraints?
John D'Errico
on 1 Mar 2018
Please don't add answers just to make a comment. Moved from an answer:
"Oh yeah - this will be part of an experiment with hundreds of trials. AR will be constant across all of them.
On each trial, I will specify what TM and N are for that trial, and I will never specify impossible values. In most cases, N = 4. In fewer cases, N = 6. But in all cases, the a given trial's ™ will be computable given AR and N."
Accepted Answer
More Answers (1)
Jeff Miller
on 1 Mar 2018
0 votes
Well, this is pretty ugly, but you could just repeatedly sample N numbers from AR randomly, stopping when they give you TM. That might take a while, but maybe you could find the numbers giving the desired (N,TM) pairs in advance (i.e., have them all ready when it was time to start your experiment).
1 Comment
elric elias
on 1 Mar 2018
Categories
Find more on Loops and Conditional Statements 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!