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!