Average of a column without including a certain number

1 次查看(过去 30 天)
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
the cyclist 2020-7-5
编辑:the cyclist 2020-7-5
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 个评论
Image Analyst
Image Analyst 2020-7-5
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.

请先登录,再进行评论。


madhan ravi
madhan ravi 2020-7-5
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 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by