Hi,
I have been trying to convert the array type to Int for
a = [3.0 2.0;2.0 4.0;7.0 8.0]
I tried convert function and used loops but have not received the required output:
a = [3 2;2 4;7 8]
Thanks
Hi,
I have been trying to convert the array type to Int for
a = [3.0 2.0;2.0 4.0;7.0 8.0]
I tried convert function and used loops but have not received the required output:
a = [3 2;2 4;7 8]
Thanks
round.(Int, a)
You can use convert
:
a = [3.0 2.0;2.0 4.0;7.0 8.0]
b = convert(AbstractArray{Int}, a)
Note that you cannot change the element type of an existing array, you must always allocate a new one if you want it to have a different type. But you can of course still use the same variable name for the new array (a = convert(AbstractArray{Int}, a)
).
Or
convert.(Int, a)
Or maybe just
Int.(a)
For completeness, there is also
julia> trunc.(Int,a)
3×2 Array{Int64,2}:
3 2
2 4
7 8
It will always round toward zero.
You have to decide what you want to happen when one of your floats isn’t an integer. What should happen to [1.0, 2.1]
? Should it round to some integer value or throw an error. The correct solution depends on what you want.