John
John

Reputation: 3070

How to compute average value of element bigger than zero in matrix

I want to compute the average value of elements in each column that bigger than zero in matrix that defined as:

G =

     1     2     3     0     9     4
     0     1     3     4     0     0

If a element is zerro, we will ignore it and does not consider in average values. My expected result is

MeanG= 1/1  3/2  6/2  4/1  9/1 4/1

How to do it by matlab code?

Upvotes: 2

Views: 1253

Answers (3)

GameOfThrows
GameOfThrows

Reputation: 4510

there are many ways to do it, you can find the number of zeros in your matrix and remove it when you are calculating the mean

z = size(A(find(A<0)))

if your other numbers are all positive, you can directly do something like

mean = sum(G)./size(G(find(G>0)),1)

Upvotes: 1

rayryeng
rayryeng

Reputation: 104504

For a rather simple solution, if you have the Statistics Toolbox, simply replace all zeros and negative values with NaN then use nanmean.

Therefore:

>> Gnan = G;
>> Gnan(Gnan <= 0) = NaN;
>> out = nanmean(Gnan)

out =

    1.0000    1.5000    3.0000    4.0000    9.0000    4.0000

I made a copy of G because I'm assuming that you want to keep the original version of G for any further analysis other than computing for the mean this way.


If you don't have access to nanmean, what you can do is take a look at each column and determine how many zeroes and negatives there are. Once you do this, simply sum up all values within each column that are not zero and negative, and divide by the total of number of values that are not zero and negative in each column. Something like:

>> zero_neg = G <= 0;
>> Gcopy = G;
>> Gcopy(zero_neg) = 0;
>> out = sum(Gcopy) ./ (size(G,1) - sum(zero_neg))

out =

    1.0000    1.5000    3.0000    4.0000    9.0000    4.0000

The intricacy here is that we search for those elements that are zero or negative, then make a copy of G and set these elements in this copy to zero so that these entries don't get added into the sum. You have to account for the correct mean by dividing by the total number of entries that are not zero or negative (or just positive actually... see Nras's post).

Note that I'm also keeping a copy of G and mutating this copy to compute our mean as I'm assuming you'll want to keep the original version of G for further analysis.

Minor Note

Jubobs made a very good point. If this matrix contains floating point numbers, it's very dangerous to compare with such a definite number like 0 due to precision and accuracy. For example, if this were a matrix where there are elements that you expect to be zero, but they aren't due to floating point imprecision, then this will not accurately calculate the mean you desire. Take a look at this post and this great answer by @gnovice for more details: Why is 24.0000 not equal to 24.0000 in MATLAB?

Upvotes: 5

Nras
Nras

Reputation: 4311

The default solution without Toolbox-Dependency would probably read:

G(G<0) = 0; % // not needed if G contains only positive numbers as in your example
sum(G, 1)./sum(G~=0, 1)

ans =

1.0000    1.5000    3.0000    4.0000    9.0000    4.0000

We sum up manually but only divide by the number of non-zero elements. In order to also sum over the correct dimension for 1-column arrays, one should also specify the dimension.

Please note that this approach probably fails for columns, which only contain zeros (or negative values)

Upvotes: 3

Related Questions