Finding averages excluding a number

5 Ansichten (letzte 30 Tage)
Michael
Michael am 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 Kommentar
Image Analyst
Image Analyst am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by