How can I end a for loop when a specific value is reached and create a new array with data that meets that condition?
1 次查看(过去 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
0 个评论
采纳的回答
更多回答(2 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Log Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!