Randomly select values from a vector that yield a target mean
古いコメントを表示
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 件のコメント
Jos (10584)
2018 年 3 月 1 日
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
2018 年 3 月 1 日
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."
採用された回答
その他の回答 (1 件)
Jeff Miller
2018 年 3 月 1 日
0 投票
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).
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!