Take values greater than 0 excluding NaN

26 visualizaciones (últimos 30 días)
federico nutarelli
federico nutarelli el 21 de Jul. de 2022
Respondida: Steven Lord el 21 de Jul. de 2022
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

Respuesta aceptada

Steven Lord
Steven Lord el 21 de Jul. de 2022
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.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by