How can I end a for loop when a specific value is reached and create a new array with data that meets that condition?

조회 수: 3 (최근 30일)
I'm trying to simulate the loss of the highest values in a dataset. My end goal is to create a new array that matches the mean of observed data -- i.e. I need to calculate means for modeled values in cells 1-2, 1-3, 1-4... 1-n until I find the range of modeled values with a mean that matches my observed data. Right now, I'm trying to work with a for loop that will terminate once a mean threshold is met -- when that threshold is met, I would like to create a new array (rh2 in my code) that incorporates the cells used to calculate the modeled mean that best matches my observed mean. My code seems giving me output of a modeled mean that matches my observed mean, but not the output of the array that I need. Thanks!
meanr2 = mean(ruber2);
ko = fitdist(ruber1,'kernel');
ruber = ko.random(500,1);
sort_ruber = sort(ruber);
for k=1:500
mur = mean(sort_ruber(1:k));
if mur <= meanr2
rh2 = ruber(1:k);
elseif mur > meanr2
return
end
end

채택된 답변

Brigitta Rongstad
Brigitta Rongstad 2017년 8월 25일
actually your answer helped me figure it out -- thank you! I need to output the sort_ruber rather than the ruber.

추가 답변 (2개)

KL
KL 2017년 8월 25일
If I understand correctly, you want to compare meanr2 with the mean calculated from ruber but you're actually comparing it with the mean of sort_ruber which is not the same and then you make a copy from ruber.
when you sort, the values in the array are rearranged.
>> ruber = rand(10,1)
ruber =
0.7803
0.3897
0.2417
0.4039
0.0965
0.1320
0.9421
0.9561
0.5752
0.0598
>> sort_ruber = sort(ruber)
sort_ruber =
0.0598
0.0965
0.1320
0.2417
0.3897
0.4039
0.5752
0.7803
0.9421
0.9561
so the mean you calculate from 1:k is not the same for both vectors. Change
mur = mean(sort_ruber(1:k));
to
mur = mean(ruber(1:k));
then it should give you what you want.

Brigitta Rongstad
Brigitta Rongstad 2017년 8월 25일
I need to calculate the mean iteratively by removing the highest value one at a time (or by adding a higher value one at a time) -- I sorted because this was the way I could think of trying to calculate a mean for values lowest-second lowest, lowest-third lowest... on until I'm calculating the mean for the lowest-highest values. Once I find the range of values (i.e. lowest to highest value range that gives me the mean I'm looking for), I need to output that value range.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by