A matrix A(100x1000) a selective column defined in B(1x1000). The required processing is the mean of non zero elements of each column of A referenced by B. Without the use of loops.
Regards

 채택된 답변

Matt J
Matt J 2021년 4월 26일
편집: Matt J 2021년 4월 26일

1 개 추천

A(~A)=nan;
result=mean(A(:,B),1,'omitnan')

댓글 수: 2

Omar Ali Muhammed
Omar Ali Muhammed 2021년 4월 26일
Dear, it is wonderful.
How can we 'omitnan' if we replace mean by trimmean?
Matt J
Matt J 2021년 4월 26일
편집: Matt J 2021년 4월 26일
Dear, it is wonderful.
I'm very glad. Please Accept-click the answer to indicate so.
How can we 'omitnan' if we replace mean by trimmean?
What would you be excluding?Zeros again? If so,
A(~A)=inf;

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

추가 답변 (1개)

Jan
Jan 2021년 4월 26일

1 개 추천

AB = A(:, B);
result = sum(AB, 1) ./ sum(AB ~= 0, 1);
mean('omitnan') replaces the NaNs by zeros for the summation and calculates the number of non-NaNs by sum(~isnan(AB)). Therefore this code should be faster, because it avoids replacing zeros by NaNs and back to zeros again.

댓글 수: 1

Matt J
Matt J 2021년 4월 26일
편집: Matt J 2021년 4월 26일
because it avoids replacing zeros by NaNs and back to zeros again.
+1. Although, I would argue, they probably should have been NaNs instead of zeros from the very beginning.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 4월 26일

편집:

2021년 4월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by