imantha
imantha

Reputation: 3828

The not operator (!) isn't working with array broadcasting

I am wondering why the not operator isn't working with array broadcasting/element-wise operations. For example, if I enter

 x = Array{Any}([1,2,3,4,missing,6])
 !ismissing.(x)

I get an error ERROR: MethodError: no method matching !(::BitArray{1})

but if I try ismissing.(x) it works fine,

ismissing.(x)
#out > 6-element BitArray{1}: 0 0 0 0 1 0

and typing without the broadcasting works as a whole as well (one output for the entire array)

!ismissing(x)
#out > True

So I am wondering if there is a possibility to get a similar result as using the "!" operator.

Upvotes: 6

Views: 792

Answers (1)

fredrikekre
fredrikekre

Reputation: 10984

You need to also broadcast ! to each element:

julia> x = Array{Any}([1,2,3,4,missing,6]);

julia> .!(ismissing.(x)) # the . comes before operators (!, +, -, etc)
6-element BitVector:
 1
 1
 1
 1
 0
 1

For this specific case you can instead negate the function before broadcasting:

julia> (!ismissing).(x)
6-element BitVector:
 1
 1
 1
 1
 0
 1

Upvotes: 14

Related Questions