# Average of a column without including a certain number

조회 수: 5(최근 30일)
Ferhat Sever 4 Jul 2020
댓글: Image Analyst 5 Jul 2020
Is there a function for this or do I have to work with loops?
I have to calculate the average of a column without counting a specific number.
x = mean (A, 1)
Just ignoring a certain number. As with the function mean (nonzeros (x)), where you ignore the 0s.

댓글을 달려면 로그인하십시오.

### 답변(2개)

the cyclist 5 Jul 2020
편집: the cyclist 5 Jul 2020
There is no specific function for this, but it is easy to do without a loop:
A = [6; 3; 6; 4];
N = 6;
mean_A_without_N = mean(A(A~=N),1)
This uses logical indexing to identify the elements to be averaged.

#### 댓글 수: 3

This forces the matrix into a column vector.
Ferhat Sever 5 Jul 2020
thank you it worked :)
Image Analyst 5 Jul 2020
It creates a temporary column vector when it does this A(A~=N). But the original A is never changed at all. The 1 in mean in not needed since it's a vector (regardless of what direction):
A = [6; 3; 6; 4];
N = 6;
mean_A_without_N = mean(A(A~=N))
It also (reasonably) assumes that you already have the column vector as A. If you don't, and you need to extract it from a 2-D matrix, you can do something like this to extract the 3rd column:
A = M(:, 3); % Extract third column (for example) from matrix M into a column vector.

댓글을 달려면 로그인하십시오.

certain_number = 5; % example
A(A == certain_number) = NaN; % use A(ismember(A, certain_numbers)) = nan if certain_numbers is more than one
M = mean(A,'omitnan')
% or
M = nanmean(A)

#### 댓글 수: 3

Steven Lord 5 Jul 2020
If you wanted to do this without modifying your original array you could use standardizeMissing.
A = [6 5 4; 3 6 2; 6 1 1]
mean(standardizeMissing(A, 6), 'omitnan') % [3, (5+1)/2, (4+2+1)/3]