# How to efficiently determine if a column of a matrix is made of integers

**URL:** <https://discourse.julialang.org/t/how-to-efficiently-determine-if-a-column-of-a-matrix-is-made-of-integers/88217>\
**Category:** General Usage\
**Tags:** question, type\
**Created:** [October 4, 2022, 9:00am UTC](https://discourse.julialang.org/t/how-to-efficiently-determine-if-a-column-of-a-matrix-is-made-of-integers/88217 "2022-10-04T09:00:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [October 4, 2022, 9:00am UTC](https://discourse.julialang.org/t/how-to-efficiently-determine-if-a-column-of-a-matrix-is-made-of-integers/88217/1 "2022-10-04T09:00:09Z")

</div>

Which is the most efficient way to determine if the columns of a matrix are all integers, e.g. `a = [1 1.5 2; 10 3.1 3]` columns 1 and 3 ?

Note that if I would have defined `a = Any[1 1.5 2; 10 3.1 3]` I could have used

```julia
julia> identity.(a[:,1])
2-element Vector{Int64}:
  1
 10 

```

and then use `eltype`, but it doesn’t work if `a` has been already cast to a matrix of Floats and I don’t know its content a priori.

I can use try/catch with `convert`, but I feel it would be even more inefficient…

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [October 4, 2022, 9:01am UTC](https://discourse.julialang.org/t/how-to-efficiently-determine-if-a-column-of-a-matrix-is-made-of-integers/88217/2 "2022-10-04T09:01:25Z")

</div>

```julia
julia> isinteger.(Any[1 1.5 2; 10 3.1 3])
2×3 BitMatrix:
 1 0 1
 1 0 1

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [October 4, 2022, 9:06am UTC](https://discourse.julialang.org/t/how-to-efficiently-determine-if-a-column-of-a-matrix-is-made-of-integers/88217/3 "2022-10-04T09:06:07Z")

</div>

Thanks, I wasn’t aware of the specific `isinteger` function. Contrary to `identity` it works also for `Float` converted matrices:

```julia
julia> a = [1 1.5 2; 10 3.1 3]
2×3 Matrix{Float64}:
  1.0 1.5 2.0
 10.0 3.1 3.0

julia> all(isinteger.(a[:,1]))
true

```

---

<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:** [October 4, 2022, 9:26am UTC](https://discourse.julialang.org/t/how-to-efficiently-determine-if-a-column-of-a-matrix-is-made-of-integers/88217/4 "2022-10-04T09:26:01Z")

</div>

> [@sylvaticus](#):
>
> `julia> all(isinteger.(a[:,1]))`

Better try to avoid slicing and broadcasting, by using the `all(func, arr)` version, with a dimension keyword:

```julia
julia> a = [1 1.5 2; 10 3.1 3]
2×3 Matrix{Float64}:
  1.0 1.5 2.0
 10.0 3.1 3.0

julia> all(isinteger, a; dims=1)
1×3 Matrix{Bool}:
 1 0 1

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [October 4, 2022, 2:52pm UTC](https://discourse.julialang.org/t/how-to-efficiently-determine-if-a-column-of-a-matrix-is-made-of-integers/88217/5 "2022-10-04T14:52:14Z")

</div>

Actually, this didn’t directly work for me, as in some cases I have string.

I solved with:

```julia
isinteger_bml(_) = false
isinteger_bml(_::Integer) = true
isinteger_bml(x::AbstractFloat) = isinteger(x)

```

I don’t know why there isn’t an `isinteger()` defaulting to false for all non integer types (except `Nothing` and `Missing`)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 4, 2022, 4:33pm UTC](https://discourse.julialang.org/t/how-to-efficiently-determine-if-a-column-of-a-matrix-is-made-of-integers/88217/6 "2022-10-04T16:33:01Z")

</div>

> [@sylvaticus](#):
>
> I don’t know why there isn’t an `isinteger()` defaulting to false for all non integer types

Because we have no way of know that’s correct for novel numeric types. It would arguably be fine for non-numeric types, but then again why are you calling `isinteger` on values that aren’t even numbers?

---

<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:** [October 4, 2022, 5:18pm UTC](https://discourse.julialang.org/t/how-to-efficiently-determine-if-a-column-of-a-matrix-is-made-of-integers/88217/7 "2022-10-04T17:18:41Z")

</div>

> [@sylvaticus](#):
>
> `isinteger_bml(_::Integer) = true`

You can skip the variable entirely, like this:

```julia
isinteger_bml(::Integer) = true

```

> [@sylvaticus](#):
>
> I don’t know why there isn’t an `isinteger()` defaulting to false for all non integer types

You mean non-numeric? I think it makes more sense to assume that asking if e.g. a `Dict` is an integer is a bug, rather than a just returning `false`.
