Take values greater than 0 excluding NaN
조회 수: 11 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
B = A;
B(B <= 0) = 0;
B(B > 0) = 1
Or:
C = discretize(A, [-Inf, eps(0), Inf], [0, 1])
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!