Finding averages excluding a number
4 views (last 30 days)
Show older comments
Hi,
I'm having a bit of trouble finding the average value of a matrix. Its a 1000x10 matrix, and I want to find the average of each column, only I don't want to include a certain number (say 99 for example). So I want to find the average of each column excluding any cell which contains the number 99. for example, say column 1 is as follows: 5 5 99 10 I want to exclude 99 so that the average comes out at 6.67 Could anyone help me? Thank you!
1 Comment
Image Analyst
on 22 Sep 2011
I hope that's an integer array, otherwise you should be aware of the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
Accepted Answer
Walter Roberson
on 22 Sep 2011
exclude = 99;
numexcluded = sum(X == exclude);
colavgs = (sum(X) - numexcluded * exclude) ./ (size(X,1) - numexcluded);
Note: this code will not work if the value to be excluded is NaN or one of the infinities.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!