Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

julia

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.

like image 396
imantha Avatar asked Feb 19 '21 09:02

imantha


1 Answers

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
like image 136
fredrikekre Avatar answered Nov 14 '22 04:11

fredrikekre