How to calculate average with taking into account only certain values

Hello,
I'm working with a large matrix that contains a lot of zero values.When I transform this matrix from a list table to a cross table, I have more zero values in it.I want to keep all the initial values in it and and supress all the zero values added from matlab for the calculation of the average.Here is an example of what I want to do:
x=
0.6000 0.6000 5.0000
0.6000 0.8000 6.0000
0.6000 0.8500 0
0.6000 0.8200 0
0.8000 0.8000 9.0000
0.8000 0.9000 2.0000
List table to cross table
NaN 0.6000 0.8000 0.8200 0.8500 0.9000
0.6000 5.0000 6.0000 0 0 0 Average=(5+6+0+0)/4=2,75
0.8000 0 9.0000 0 0 2.0000 Average=(9+2)/2=5,5
Thanks in advance

Risposte (1)

I don't know what the cross table stuff means, but to get the mean of an array going across columns, you can do this:
xRowMeans = sum(x, 2) ./ sum(x ~= 0, 2)

5 Commenti

It's just a way to rearrange values:
A=
x y z
1 1 5
1 2 6
1 3 7
2 1 8
2 2 9
2 3 2
A=
y->
x 1 2 3
1 5 6 7 <-z values
2 8 9 2
Well, whatever. x can be any 2D matrix whatsoever in my formula: A, y, x, whatever - it doesn't matter. Does it answer your question about how to get means without considering zeros?
It answers in part to my question, because I want to supress certain zeros in the calcul , but not those initially present in the matrix.I want to delete only zeros added by matlab.
I don't know what the calcul is but you can do this:
rowMeans = sum(calcul, 2) ./ sum(calcul ~= 0, 2)

Accedi per commentare.

Richiesto:

il 31 Mar 2014

Commentato:

il 3 Apr 2014

Community Treasure Hunt

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

Start Hunting!

Translated by