Calculate mean of certain values in a vector

9 次查看(过去 30 天)
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!

采纳的回答

Star Strider
Star Strider 2019-9-25
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by