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?
You might be looking for
julia> Int.([2.9,3.9,4.5].>0)
3-element Array{Int64,1}:
1
1
1
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
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
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.
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
Hi @nilshg ,
I saw this topic, and currently have facing the similar problem.
I have this vector of LU decomposition:
3-element Vector{Float64}:
** 1.7499999999999998**
** 0.9999999999999998**
** -0.4999999999999999**
I do this:
#Change result to Int
I = Int.(x.>0)
Get this:
3-element Vector{Int64}:
** 1**
** 1**
** 0**
Since -0.499n9999999 is too far to be rounded to 0, I want to round it to 3 significant digits, try this:
round(I; digits = 3)
failed:
MethodError: no method matching round(::Vector{Int64}; digits=3)
Closest candidates are:
Hi Freya, probably best to start a new topic rather than resurrect 3-year old threads and ping everyone who answered here, but in this specific case the solution already provided above is also the solution to your problem: round
works on scalars, so if you want to apply it elementwise to an array you need to do round.(I)
.
Thanks @nilshg , sorry for pinging after 3 years this thread is dead. I thought it is better to ask here than start a new one, guess I was wrong.
Thanks again your guide on Jupyter Notebook helps me to make Julia Kernel alive again
(https://stackoverflow.com/questions/62071591/kernel-error-using-julia-on-jupyter-notebook)