hi all
if i have array c = [1.0, 2.0, 2.0, 1.0] and i want to convert it to Int in this way c = [1,2,2,1]
i tried some thing like that
for i in count
i = Int64(i)
println(i) # the output her is 1 then 2 => like what i need
end
println(count) # the output her is in float64 it didn't change
how i can solve that ?
and one more thing please
and if i need to Define an array
how i can do that ?
Suppose i need array with size 26
c = [1.0, 2.0, 2.0, 1.0]
println(typeof(c))
c = Int.(c) #Change from Float to Int
println(typeof(c))
The dot between Int and c tells Julia to apply the Int conversion elementwise to the array.
Can you be more specific with what you are looking for for an array? If you are just looking to allocate space for a 26 element vector, you can do that by: Array{Any, 1}(undef, 26)
The Any tells Julia the type of the Array, the 1 tells Julia the dimension of the matrix(above gives you a vector), and undef would allocate the space, but not populate it with anything.
You are looking for isinteger. But generally this is likely not a good approach. If you want Ints just use round(Int, x) or trunc(Int, x) or any related function. A mixture of exact Ints and floats is likely not very useful or meaningful.
Also please don’t resurrect ancient topics (half a decade in this case). If you have questions, just open a new topic (and link back if you think the context is still relevant).
Well, I understood that isinteger is by far the better option than my x % 1 == 0. But if we’re already continuing the discussion here, I’d like to add another version:
function int_if_possible(v::AbstractArray{T, N}) where {T, N}
all(isinteger.(v)) ? Array{Int, N}(v) : v
end
which can handle multiple dimensions as well as empty arrays.
Moreover, here’s a benchmark on various versions. Interestingly, the broadcasting one is the fastest.
function int_if_possible_1(v)
all(v .% 1 .== 0) ? Int.(v) : v
end
function int_if_possible_2(v::AbstractArray{T, N}) where {T, N}
all(isinteger.(v)) ? Array{Int, N}(v) : v
end
function int_if_possible_3(v::AbstractArray{T, N}) where {T, N}
all(isinteger, v) ? Array{Int, N}(v) : v
end
function int_if_possible_4(v::AbstractArray{T, N}) where {T, N}
all_integer = true
for x in v
isinteger(x) || (all_integer=false; break)
end
all_integer ? Array{Int, N}(v) : v
end
v = 1:1.0:100_000
using BenchmarkTools
@btime int_if_possible_1(v);
# 981.000 μs (5 allocations: 793.61 KiB)
@btime int_if_possible_2(v);
# 350.400 μs (5 allocations: 793.61 KiB)
@btime int_if_possible_3(v);
# 477.300 μs (3 allocations: 781.32 KiB)
@btime int_if_possible_4(v);
# 475.200 μs (3 allocations: 781.32 KiB)
That wasn’t my point. My main point was that it is generally not a good idea to create intermediate arrays needlessly. So I suggest all(isinteger, v) instead of all(isinteger.(v)) to avoid allocations.
It can happen in some cases that allocating is faster, but generally, the non-allocating version is better.
Also, for this benchmark you should use variable interpolation for more accurate results, ie.
Don’t worry, I did understood that and I was surprised to see that the allocating version was faster. That’s why I mentioned it. Maybe I should have been clearer.
My main point, however, was the handling of multiple dimensions and empty arrays.