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!