Calculate mean of certain values in a vector
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Daniel Miller
le 25 Sep 2019
Commenté : Carlos Maqueda
le 27 Mar 2020
Hi!
I have two problems I'd need some help with.
I have a column vector (25 x 1) with positive and negative values. It has the following values:
1716000
0
-2400000
-5124000
880000
-51000
-192000
656000
165000
702000
1456000
1700000
-550000
46000
8272000
-660000
-162000
2000
22000
210000
2817000
285000
-1400000
840000
570000
First problem: Out of these values, I would first like to calculate the average (mean) of all the values that are not negative.
I tried the following script, but it gives a weird answer of 0,68:
OnlyPositives = profit>=0;
avgProfitNoLoss = mean(OnlyPositives)
I don't know why it gives me the answer of 0,68 so the first question is how I can correct this.
The next step then is to calculate the average of the values that are zero and the values that are negative. But, the first problem above needs to be addressed first.
Thank you in advance for any help/suggestions!
0 commentaires
Réponse acceptée
Star Strider
le 25 Sep 2019
You created a logical vector with ‘OnlyPositives’. Use it to calculate the mean as:
avgProfitNoLoss = mean(profit(OnlyPositives))
producing:
avgProfitNoLoss =
1196411.76470588
that is likely the result you want.
The average of the values that are zero will be zero, so you can avoid that calculation.
3 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Multirate Signal Processing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!