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

采纳的回答

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