简体   繁体   English

求非零元素的平均值

[英]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. 我假设mean函数采用矩阵并通过对数组的所有元素求和来计算其均值,并将其除以元素的总数。

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. 然后我遇到了一个我不希望平均函数考虑我的矩阵的0个元素的点。 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? 具体来说,我的矩阵是1x100000数组,并且它的元素的1/3到1/2都是0.如果是这种情况,我可以用NULL替换0元素,以便matlab在计算时不会考虑它们均值? What else can I do? 我还可以做些什么?

Short version: 精简版:
Use nonzeros : 使用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. 如果您正在使用具有100K条目的数组,并且大量这些条目为0,则可以考虑使用sparse表示。 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() ). 您可以通过将值相加并除以非零元素的数量( 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 对于二维情况(或n维情况),您必须指定求和应发生的维度

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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