Finding averages excluding a number

5 vues (au cours des 30 derniers jours)
Michael
Michael le 22 Sep 2011
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 commentaire
Image Analyst
Image Analyst le 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

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 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.
  1 commentaire
Michael
Michael le 22 Sep 2011
Thanks, much appreciated!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by