Scalar contained by vector of StaticArrays

I want to check if an integer is contained by a vector of StaticArrays. Here’s an example (with regular arrays, but it’s the same issue).

a = [[1,2], [3,4]]
1 in a # false

I guess I could write a for loop, but that’s not very elegant.

  • The number I’m looking for will always be in the first “column” (i.e., first position of a subarray).
  • If the number is in a, I will need to find it’s position, something like this: findfirst(x->x==1,a), which doesn’t work in this case either.

Thanks a lot!
PS: this is a silly question, but a difficult one to Google. The word “in” is a bit too common.

julia> any(∋(1), a)

julia> findfirst(∋(1), a)

note: this only handles one level of nest-ness

1 Like

That doesn’t seem to work for me.

julia> any(∋(1), a)
ERROR: MethodError: no method matching ∋(::Int64)
Closest candidates are:
  ∋(::Any, ::Any) at operators.jl:1069
 [1] top-level scope at REPL[14]:1

I’m also having some trouble with the character. Launching Julia in Windows it looks like a square with a question mark in the middle, while in Linux and in VS Code it does appear. Does this function have another name?
I’m using Julia Version 1.5.3.

yeah got added “recentlly”

In the meantime you can use any(v -> 1 ∈ v, a)

Thank you both!
For now:

any(v -> 1 ∈ v, a)
findfirst(v -> 1 ∈ v, a)

Works for me!

1 Like