Convert float 2-D array to integer 2-D array in Julia

10,999

Solution 1

This answer is for Julia v0.3. For newer versions, see answer of DSM

Use the int function:

julia> a = rand(2,2)
2x2 Array{Float64,2}:
0.145651  0.362497
0.879268  0.753001
julia> int(a)
2x2 Array{Int64,2}:
0  0
1  1

Solution 2

You can convert a 2x2 array of floats into a 2x2 array of ints very easily, after you decide how you want rounding to be handled:

julia> A = [1.0 -0.3; 3.9 4.5]
2x2 Array{Float64,2}:
 1.0  -0.3
 3.9   4.5
julia> round.(Int, A)
2x2 Array{Int64,2}:
 1  0
 4  4
julia> floor.(Int, A)
2x2 Array{Int64,2}:
 1  -1
 3   4
julia> trunc.(Int, A)
2x2 Array{Int64,2}:
 1  0
 3  4
julia> ceil.(Int, A)
2x2 Array{Int64,2}:
 1  0
 4  5

Solution 3

You can use map, which preserves the matrix dimensions, and does not depend on vectorized methods:

julia> x = rand(2,2)
2x2 Array{Float64,2}:
 0.279777  0.610333
 0.277234  0.947914
julia> map(y->round(Int,y), x)
2x2 Array{Int64,2}:
 0  1
 0  1
Share:
10,999
I Like to Code
Author by

I Like to Code

Updated on July 24, 2022

Comments

  • I Like to Code
    I Like to Code 5 months

    I know that it is possible to convert a Float64 into an Int64 using the convert function. Unfortunately, it doesn't work when applying convert to a 2-D array.

    julia> convert(Int64, 2.0)
    2
    julia> A = [1.0 2.0; 3.0 4.0]
    2x2 Array{Float64,2}:
     1.0  2.0
     3.0  4.0
    julia> convert(Int64, A)
    ERROR: `convert` has no method matching convert(::Type{Int64}, ::Array{Float64,2
    })
     in convert at base.jl:13
    

    How do I convert a 2-D array of floats into a 2-D array of ints?

    What I tried

    I could do it using the following code, which is a little verbose but it works. I am hoping there is an easier way to do it though.

    julia> A = [1.0 2.0; 3.0 4.0]
    2x2 Array{Float64,2}:
     1.0  2.0
     3.0  4.0
    julia> B = Array(Int64, 2, 2)
    2x2 Array{Int64,2}:
     4596199964293150115  4592706631984861405
     4604419156384151675                    0
    julia> for i = 1:2
               for j = 1:2
                   B[i,j] = convert(Int64,A[i,j])
               end
           end
    julia> B
    2x2 Array{Int64,2}:
     1  2
     3  4
    

    An answer that doesn't work for me

                   _
       _       _ _(_)_     |  A fresh approach to technical computing
      (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
       _ _   _| |_  __ _   |  Type "help()" for help.
      | | | | | | |/ _` |  |
      | | |_| | | | (_| |  |  Version 0.3.10 (2015-06-24 13:54 UTC)
     _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org release
    |__/                   |  x86_64-linux-gnu
    julia> A = [1.2 3.4; 5.6 7.8]
    2x2 Array{Float64,2}:
     1.2  3.4
     5.6  7.8
    julia> round(Int64, A)
    ERROR: `round` has no method matching round(::Type{Int64}, ::Array{Float64,2})
    
  • I Like to Code
    I Like to Code over 7 years
    Do you know why I get the output julia> round([1.2 3.4], Int64) ERROR: 'round' has no method matching round(::Array{Float64,2}, ::Type{Int64})
  • DSM
    DSM over 7 years
    @ILiketoCode: you're doing it the wrong way around.
  • I Like to Code
    I Like to Code over 7 years
    See my edited question where I try it the right way round and it still doesn't work...
  • DSM
    DSM over 7 years
    @ILiketoCode: I'm not sure what's going on, then -- I just tried it again with a fresh build, Version 0.4.0-dev+6177 (2015-07-22 19:39 UTC), and it worked just fine, and gave me 2x2 Array{Int64,2}: 1 3 6 8 on your example.
  • mbauman
    mbauman over 7 years
    It's new functionality in 0.4. On 0.3 you can do convert(Array{Int}, round(A)).
  • I Like to Code
    I Like to Code over 7 years
    Is there a stable release for 0.4? It seems the latest release is 0.3.10
  • I Like to Code
    I Like to Code over 7 years
    I'm accepting this answer because it is the easiest to type.
  • mbauman
    mbauman over 7 years
    That's correct. This will be the preferred syntax once 0.4 is released (soon). Until then, I'd recommend using the Compat.jl package, which enables the use of these new methods simply by using Compat.
  • mbauman
    mbauman over 7 years
    This is deprecated on the soon-to-be-released 0.4 in favor of the methods in @DSM's answer, which explicitly state how to convert to Int.
  • Andrew McKinlay about 6 years
    Note that these functions can still throw an InexactError(). For example, round(Int64, convert(Float64, typemax(Int64))).
  • keiv.fly
    keiv.fly over 5 years
    On Julia 0.5: ERROR: UndefVarError: int not defined
  • Hansang
    Hansang almost 3 years
    This should not be the accepted answer anymore since it clearly doesn't work
  • Hansang
    Hansang almost 3 years
    I've tested this implementation and the broadbasting methods in DSM's answer using btime, and in runtime they are similar in performance a = rand(10000,10000) (at)btime map(y->round(Int,y), a) # 501.133 ms vs (at)btime round.(Int, a) #489.392 ms