필터 지우기
필터 지우기

Remove rows or cols whose elements are all NaN

조회 수: 4 (최근 30일)
Jeon
Jeon 2013년 4월 2일
I have a matrix A:
A = [1 2 3 4 NaN;
NaN NaN NaN NaN NaN;
1 2 NaN 4 5;
1 NaN 3 4 5];
and what I just want to is:
A = [1 2 3 4 NaN;
1 2 NaN 4 5;
1 NaN 3 4 5];
Currently I'm using the follwoing:
method = 1;
A = A'; % transposing is just for convenience
if method == 1
A = A(:, ~isnan(nanmean(A))); % nanmean throws NaN if all elements are NaN
elseif method == 2
A = A(:, sum(~isnan(A)) ~= 0);
end
A = A'; % re-transpose
However, I think nanmean is for statistical purpose and not for matrix manipulation. Moreover, actually I have much much much larger matrix. Thus, such operation might require longer computational time.
And I don't think all(isnan(A)) or all(~isnan(A)) is appropriate in some special cases.

채택된 답변

José-Luis
José-Luis 2013년 4월 2일
your_mat = A(all(isnan(A),2),:);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by