# How do I check if a variable is an array of numbers

**URL:** <https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734>\
**Category:** New to Julia\
**Tags:** numbers, type\
**Created:** [September 25, 2021, 1:38am UTC](https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734 "2021-09-25T01:38:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 25, 2021, 1:38am UTC](https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734/1 "2021-09-25T01:38:15Z")

</div>

How do I check if a variable x is a subtype of an array of numbers

```julia
x = [1.1, 1.2, 1.3]

julia> typeof(x) == Array{Float64, 1} 
true

julia> typeof(x) == Array{Number, 1}
false

julia> typeof(x) <: Array{Number, 1}
false

julia> isa(x,Array{Number, 1})
false

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 25, 2021, 1:51am UTC](https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734/2 "2021-09-25T01:51:58Z")

</div>

```julia
julia> x = rand(3)
3-element Vector{Float64}:
 0.17437593361404558
 0.7714656320412703
 0.340420132628811

julia> x isa Array{<:Number,1}
true

```

see: [Vector{Int} \<: Vector{Real} is false??? · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/typevariance/)

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 25, 2021, 2:10am UTC](https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734/3 "2021-09-25T02:10:49Z")

</div>

> [@lmiq](#):
>
> `x isa Array{<:Number,1}`

Thank you.

I found that this works as well

```julia
if typeof(kernel) <: Array{T, 1} where T<:Number

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 25, 2021, 2:52am UTC](https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734/4 "2021-09-25T02:52:37Z")

</div>

If you’re coming from matlab, you should probably know that you don’t want to be doing these checks in functions. Instead, use dispatch and broadcasting to make things just work with arrays.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 25, 2021, 7:19am UTC](https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734/5 "2021-09-25T07:19:24Z")

</div>

I need to determine if a variable is a Number or an array of Number. It must be able to reject a string or reject an array of strings

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 25, 2021, 9:36am UTC](https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734/6 "2021-09-25T09:36:59Z")

</div>

> [@StevenSiew](#):
>
> I need to determine if a variable is a Number or an array of Number

Would this work:

```julia-auto
test(x) = x isa Union{<:Number, Array{<:Number,1}}

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 25, 2021, 9:54am UTC](https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734/7 "2021-09-25T09:54:56Z")

</div>

The idiomatic way of doing this is writing two functions. In its simplest form, that looks like this:

```julia
function myfunc(a::Int)
   # code for handling a single Int
end

function myfunc(a::Vector{Int})
    # code handling a vector containing only Ints
end

```

Now, when you call `myfunc(myvar)`, julia will choose the correct method based on the type of `myvar` and since only two methods (one taking a single `Int` and the other taking a `Vector{Int}`) exists, it won’t accept anything other than those two - it’ll throw an error when you pass it e.g. a `String`.

* * *

It should be noted however that mixing scalars and vectors like this in your code is probably not a good idea - may I ask what you’re trying to code up?

---

<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:** [September 25, 2021, 5:31pm UTC](https://discourse.julialang.org/t/how-do-i-check-if-a-variable-is-an-array-of-numbers/68734/8 "2021-09-25T17:31:34Z")

</div>

I think it’s a rare case where you as the programmer do not know if you have a scalar or an array, I don’t think that ever happens to me, at least. Can you describe how this comes about?

I would claim that the idiomatic solution is _not_ to write a method for scalar and one for Array, but to only write one for scalar and then call that with a broadcasting dot when you want to use it on an array.
