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 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Brigitta Rongstad
il 24 Ago 2017
Risposto: Brigitta Rongstad
il 25 Ago 2017
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 Commenti
Risposta accettata
Più risposte (2)
KL
il 25 Ago 2017
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 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!