Find the mean of nonzero elements

I am assuming that the function mean

takes a matrix and calculates its average by summing the entire element of the array and dividing it by the total number of elements.

However, I am using this function to calculate the average of my matrix. Then I come across a point where I don't want the middle function to consider 0 elements of my matrix. Specifically, my matrix is ​​a 1x100000 array, and maybe 1/3 to 1/2 of its element is all 0. If so, can I replace element 0 with NULL

so that Matlab does not take them into account when calculating the average? What else can I do?

+3


source to share


3 answers


Short version:
Use nonzeros

:

mean( nonzeros(M) );

      




Longer answer:
If you are working with an array with 100k records, a significant portion of those records are 0, you might consider working with sparse

. It might also be wise to store 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

      

+7


source


As you asked, "What else can I do?" There is a different approach that does not depend on the statistics dashboard or any other toolkit.

You can compute them, therefore, yourself, by summing the values ​​and dividing by the number of nonzero elements ( nnz()

). Since summing zeros does not affect the amount, this will give the desired result. For a 1-dimensional case, as you think, it can be done as follows:

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

      



For the two-dimensional case (or the n-dimensional case), you must specify the size over which the summation should occur

% // 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]

      

+4


source


To find the average of only non-zero elements, use boolean indexing to extract non-zero elements, and then call mean

on those:

mean(M(M~=0))

      

+3


source







All Articles