Calculate mean of certain values in a vector

12 Ansichten (letzte 30 Tage)
Daniel Miller
Daniel Miller am 25 Sep. 2019
Kommentiert: Carlos Maqueda am 27 Mär. 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!

Akzeptierte Antwort

Star Strider
Star Strider am 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 Kommentare
Star Strider
Star Strider am 25 Sep. 2019
As always, my pleasure!
Carlos Maqueda
Carlos Maqueda am 27 Mär. 2020
I had the same problem, working with digital signs (+,-). Thanks too

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by