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?
mat(mat==0)=NaN
. Then usenanmean
. Be careful if you are dealing with floating point numbers though, in that case you some tolerance value. If you manage to solve it, consider posting an answer on this. – Divakar Oct 29 '14 at 6:340
are precisely0
and not just a very tiny number that appears to be zero like0.00000000000000000000000001
as this will not be found when doing==0
and hence rather use something likemat(abs(mat) < tol)
wheretol
is a tiny number – Dan Oct 29 '14 at 6:40nanmean
could be elegant, as with it you can specify the dimension you would like to work with, just like you can do with standardmean
. – Divakar Oct 29 '14 at 6:47