# Bit matrix of non-NaNs

**URL:** https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376
**Category:** New to Julia
**Created:** [April 24, 2025, 3:24pm UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376 "2025-04-24T15:24:06Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Boris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boris/32/3306_2.png) [@Boris](https://discourse.julialang.org/u/Boris)
#### Post date: [April 24, 2025, 3:24pm UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376/1 "2025-04-24T15:24:06Z")

</div>

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

```julia
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

```julia
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

```julia
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.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [April 24, 2025, 3:30pm UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376/2 "2025-04-24T15:30:20Z")

</div>

You need one more dot…

```julia
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
))))

```

---

<div class="post-metadata">

### Author: ![Boris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boris/32/3306_2.png) [@Boris](https://discourse.julialang.org/u/Boris)
#### Post date: [April 24, 2025, 3:39pm UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376/3 "2025-04-24T15:39:01Z")

</div>

Missed that!

Thanks!

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 24, 2025, 7:14pm UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376/4 "2025-04-24T19:14:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Boris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boris/32/3306_2.png) [@Boris](https://discourse.julialang.org/u/Boris)
#### Post date: [April 25, 2025, 5:52am UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376/5 "2025-04-25T05:52:10Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 25, 2025, 6:08am UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376/6 "2025-04-25T06:08:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [April 25, 2025, 6:27am UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376/7 "2025-04-25T06:27:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 25, 2025, 6:51am UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376/8 "2025-04-25T06:51:51Z")

</div>

> [@jw3126](#):
>
> Dot is not only for broadcast but also for getproperty.

Yeah, that was the part I was missing.

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [April 25, 2025, 7:18pm UTC](https://discourse.julialang.org/t/bit-matrix-of-non-nans/128376/9 "2025-04-25T19:18:32Z")

</div>

> [@Boris](#):
>
> I would do this in Matlab (with `~`) and somehow I expected this to work

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