Finding mean of matrix excluding 0
Show older comments
I have a 4 1200x1200 matrixes which includes numerous 0 values, lets just call them 'a' 'b' 'c' and 'd'
How do i find the mean of each individual point in the matrix (that is a+b+c+d), while ignoring the 0 values?
Thank you for the help!
Accepted Answer
More Answers (1)
Image Analyst
on 13 May 2016
You mean the mean at each location, like (a+b+c+d)/(# non-zeros)? How about
counts = (a~=0) + (b~=0) + (c~=0) + (d~=0);
theMeans = (a+b+c+d) ./ counts;
theMeans is a 1200-by-1200 matrix where each value is the mean of up to 4 values. If all 4 values are 0 then the value there is a nan.
2 Comments
goyanks1989
on 13 May 2016
Image Analyst
on 13 May 2016
They are not all uint16 - some must be different. Cast all to double and then do it.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!