Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find mean of non-zero elements

Tags:

matlab

mean

I am assuming that the mean fucntion takes a matrix and calculate its mean by suming all element of the array, and divide it by the total number of element.

However, I am using this functionality to calculate the mean of my matrix. Then I come across a point where I don't want the mean function to consider the 0 elements of my matrix. Specifically, my matrix is 1x100000 array, and that maybe 1/3 to 1/2 of its element is all 0. If that is the case, can I replace the 0 element with NULL so that the matlab wouldn't consider them in calculating the mean? What else can I do?

like image 899
kuku Avatar asked Oct 29 '14 06:10

kuku


3 Answers

Short version:
Use nonzeros:

mean( nonzeros(M) );

A longer answer:
If you are working with an array with 100K entries, with a significant amount of these entries are 0, you might consider working with sparse representation. It might also be worth considering storing it as a column vector, rather than a row vector.

sM = sparse(M(:)); %// sparse column
mean( nonzeros(sM) ); %// mean of only non-zeros
mean( sM ); %// mean including zeros
like image 72
Shai Avatar answered Nov 15 '22 06:11

Shai


As you were asking "What else can I do?", here comes another approach, which does not depend on the statistics Toolbox or any other Toolbox.

You can compute them mean yourself by summing up the values and dividing by the number of nonzero elements (nnz()). Since summing up zeros does not affect the sum, this will give the desired result. For a 1-dimensional case, as you seem to have it, this can be done as follows:

% // 1 dimensional case
M = [1, 1, 0 4];
sum(M)/nnz(M) % // 6/3 = 2

For a 2-dimensional case (or n-dimensional case) you have to specify the dimension along which the summation should happen

% // 2-dimensional case (or n-dimensional)
M = [1, 1, 0, 4
      2, 2, 4, 0
      0, 0, 0, 1];

% // column means of nonzero elements      
mean_col = sum(M, 1)./sum(M~=0, 1) % // [1.5, 1.5, 4, 2.5]

% // row means of nonzero elements
mean_row = sum(M, 2)./sum(M~=0, 2) % // [2; 2.667; 1.0]
like image 41
Nras Avatar answered Nov 15 '22 08:11

Nras


To find the mean of only the non-zero elements, use logical indexing to extract the non-zero elements and then call mean on those:

mean(M(M~=0))
like image 37
Dan Avatar answered Nov 15 '22 07:11

Dan