Bit matrix of non-NaNs

Hi. If I have a matrix with NaNs I can find them with isnan.(). Sorry for the stupid question, but how do I get the non-NaN bit matrix?

suppose I have

YY = rand(10,3); YY[2,1] = NaN; YY[4,2] = NaN; YY[3,3] = NaN;
Sm = isnan.(YY)

how do I get the “other” matrix where i have only the non-NaN values?

I tried

So = !isnan.(YY)

but that doesn’t work. I would do this in Matlab (with ~) and somehow I expected this to work because I have seen similar synthax in Julia, for example with findall for Cartesian Indices

CI_nan =findall(isnan,YYt) # works
CI_non = findall(!isnan,YYt) # also works

but I don’t seem to find the correct syntax for the bit matrix…

Thanks.

You need one more dot…

julia> @. !isnan(YY)  # macro adds dots everywhere
10×3 BitMatrix:
 1  1  1
 0  1  1
...

julia> .!isnan.(YY)  # infix operator takes . before, like .*
10×3 BitMatrix:
 1  1  1
...

julia> (!isnan).(YY)  # uses !(::Function)
10×3 BitMatrix:
 1  1  1
...

julia> typeof(!isnan)
ComposedFunction{typeof(!), typeof(isnan)}

julia> Meta.@lower !isnan.(YY)  # failed case, broadcasts then applies ! to array
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = !
│   %2 = Base.broadcasted(isnan, YY)
│   %3 = Base.materialize(%2)
│   %4 = (%1)(%3)
└──      return %4
))))

julia> Meta.@lower .!isnan.(YY)  # one fused broadcast
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = !
│   %2 = Base.broadcasted(isnan, YY)
│   %3 = Base.broadcasted(%1, %2)
│   %4 = Base.materialize(%3)
└──      return %4
))))
4 Likes

Missed that!

Thanks!

Is it obvious why . should have higher precedence than !?

Hm, no I didn’t even think about that. Now that you mention it I did try !. yesterday but it was one of many tries didn’t think much about it.

That is a good learning experience, why?

What I meant was that it would be possible to interpret (parse) !isnan.() as (!isnan).(), but it is actually parsed as !(isnan.()). I was wondering why that should be the case.

Dot is not only for broadcast but also for getproperty. Consider !geometry.isvisible there is only one practical precedence here.

3 Likes

Yeah, that was the part I was missing.

You could use So = .~isnan.(YY).