필터 지우기
필터 지우기

Take values greater than 0 excluding NaN

조회 수: 9 (최근 30일)
federico nutarelli
federico nutarelli 2022년 7월 21일
답변: Steven Lord 2022년 7월 21일
Hi all, I have a matrix A, say, like this:
NaN NaN 1
NaN -2 -3
NaN NaN NaN
2 NaN -3
NaN -3 -3
1 -1 2
and would like to make a dummy matrix out of A having values 1 for values that are greater than 0 but ignoring missing values. In other words the expected result is:
NaN NaN 1
NaN 0 0
NaN NaN NaN
1 NaN 0
NaN 0 0
1 0 1
I tried this:
A=A(~isnan(A))>0
but it provides a vector while I would like a matrix of the same dimensions as A.
Thank you

채택된 답변

Steven Lord
Steven Lord 2022년 7월 21일
A = [NaN NaN 1;
NaN -2 -3;
NaN NaN NaN;
2 NaN -3;
NaN -3 -3;
1 -1 0] % Adding an explicit 0
A = 6×3
NaN NaN 1 NaN -2 -3 NaN NaN NaN 2 NaN -3 NaN -3 -3 1 -1 0
B = A;
B(B <= 0) = 0;
B(B > 0) = 1
B = 6×3
NaN NaN 1 NaN 0 0 NaN NaN NaN 1 NaN 0 NaN 0 0 1 0 0
Or:
C = discretize(A, [-Inf, eps(0), Inf], [0, 1])
C = 6×3
NaN NaN 1 NaN 0 0 NaN NaN NaN 1 NaN 0 NaN 0 0 1 0 0
For this last approach anything in the interval [-Inf, eps(0)) (which includes 0; the fact that the right edge is not included is why I used eps(0) instead of 0) becomes 0. Anything in the interval [eps(0), Inf] (this last bin includes both left and right edges) becomes 1. Anything in neither of those intervals (aka NaN) becomes NaN.
If you want an explicit 0 in A to be a 1, use 0 instead of eps(0) in the second input.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by