If I have an 2D array of multiple elements (named aux), how do I convert or ensure that they are integers? Also note that I didn’t create this array, it was copied from a dataframe, which was in turn taken from a CSV file. I don’t know if that is relevant but thought it might be better to mention. I tried round(Int, aux) but it doesn’t work. I get the following error:
ERROR: MethodError: objects of type Array{ConstraintRef{Model,C,Shape} where Shape<:AbstractShape where C,1} are not callable
Use square brackets [] for indexing an Array.
If T is an Integer type, an InexactError will be raised if x is not representable by T, for example if x is not
integer-valued, or is outside the range supported by T.
Thanks for the suggestion, but it doesn’t accept arrays apparerntly. So I tried using a for loop
for i = 1:100
convert(Int, aux[i])
end
I think it momentarily converts to integer but then it goes back to float. The entire array is actually integer except for one value, because I wanted to experiment. But because of that one float value, the entire array becomes float.
The element type of an array is fixed and cannot be changed. You can create a new array with the result of converting all elements to Int with e.g. Int.(aux).
ERROR: LoadError: MethodError: no method matching copyto!(::Float64, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0},Tuple{},Type{Int64},Tuple{Float64}})
Closest candidates are:
copyto!(::AbstractArray, ::Base.Broadcast.Broadcasted{var"#s826",Axes,F,Args} where Args<:Tuple where F where Axes where var"#s826"<:Base.Broadcast.AbstractArrayStyle{0}) at broadcast.jl:889
copyto!(::AbstractArray, ::Base.Broadcast.Broadcasted) at broadcast.jl:886
copyto!(::AbstractArray, ::Any) at abstractarray.jl:730
Weird, as it works when I write them out line for line (without the dot in the assignment), as in my above post…
Ah, of course, my bad, the type of the Array is concrete and you cannot change its contents this way, you really need to allocate a new Array. The solution then is:
for i in ("Array1", "Array2", "Array3")
m[i] = Int.(m[i])
end
You need to change to what m is pointing to, so you need to change it some way. This ends up in what I say in my post. There are no boxes there. i is just a name, you create a whole new array with Int.(i), and you change i to refer to it; but i is just a label, it is not a box, changing what i refer to will not update anything, it is not a memory position that is shared by m.