简体   繁体   中英

Find mean of non-zero elements

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?

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

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]

要仅查找非零元素的平均值,请使用逻辑索引来提取非零元素,然后在这些元素上调用mean

mean(M(M~=0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM