Why there is no method matching round(::Type{Int64}, ::BitArray{1})

let us suppose that i want to perform this following operation:
round(Int,[2.3,2.5,8.9].>0)
and then i get a MethodError saying that:
ERROR: MethodError: no method matching round(::Type{Int64}, ::BitArray{1}).
is there any other way to perform this operation?

I’m not sure I understand the operation you’re trying to perform - could you clarify the expected output?

[2.3,2.5,8.9].>0 should give you a Bitarray with all 1’s, what to you expect round to do with that?

3 Likes

You might be looking for

julia> Int.([2.9,3.9,4.5].>0)
3-element Array{Int64,1}:
 1
 1
 1
2 Likes

You cannot round a vector to an Int. But you can round each element of the vector to an Int:

julia> round.(Int,[2.3,2.5,8.9].>0)
3-element Array{Int64,1}:
 1
 1
 1
5 Likes

yes i was trying to round each element of the array that’s what i wanted to perform

Did you also want to filter on non-negative values? (Your original has a >0 comparison and I’m not sure why it was there.)

So perhaps one of these:

julia> round.(Int, [2.3,2.5, 8.9])
3-element Array{Int64,1}:
 2
 2
 9

julia> [round(Int,x) for x ∈ [2.3,2.5,8.9,-5.2] if x>0]
3-element Array{Int64,1}:
 2
 2
 9
3 Likes

actually my aim is to return all the positive numbers to 1, and the negatives in that array should return 0

So how about this:

julia> [x>0 ? 1 : 0 for x ∈ [2.3,2.5,8.9,-5.2]]
4-element Array{Int64,1}:
 1
 1
 1
 0

or equivalently but maybe better for reuse or composition:

julia> f(x) = x>0 ? 1 : 0
f (generic function with 1 method)

julia> f.([2.3,2.5, 8.9,-5.2])
4-element Array{Int64,1}:
 1
 1
 1
 0

and if you want your answer in place (replacing your input data) rather than making a new array you can use the element-wise assignment operator .=:

julia> a1 = [2.3,2.5, 8.9,-5.2]
4-element Array{Float64,1}:
  2.3
  2.5
  8.9
 -5.2

julia> a1 .=f.(a1)
4-element Array{Float64,1}:
 1.0
 1.0
 1.0
 0.0

Note that in the last one, because the array already exists with a type of Float64, the answers were converted to that type.

1 Like

all of these solutions are possible it’s just you have to do it in the right way that goes well with the entire code

1 Like