简体   繁体   中英

MethodError: no method matching -(::Int64, ::Array{Int64,1})

I tried playing around with this example in the Julia documentation. My attempt was to make the cell split into two parts that have half of the amount of protein each.

using OrdinaryDiffEq
const α = 0.3
function f(du,u,p,t)
  for i in 1:length(u)
    du[i] = α*u[i]/length(u)
  end
end
function condition(u,t,integrator) # Event when event_f(u,t) == 0
  1-maximum(u)
end
function affect!(integrator)
  u = integrator.u
  idxs = findall(x->x>=1-eps(eltype(u)),u)
  resize!(integrator,length(u)+length(idxs))
  u[idxs] ./ 2
  u[end-idxs:end] = 0.5
  nothing
end
callback = ContinuousCallback(condition,affect!)
u0 = [0.2]
tspan = (0.0,10.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob,Tsit5(),callback=callback)

I get the error: MethodError: no method matching -(::Int64, ::Array{Int64,1}) . I know there is a problem with idxs = findall(x->x>=1-eps(eltype(u)),u) , and my attempt was to put a dot between the 1 and eps, but that didn't fix it. I am using Julia 1.1.1.

Running your code the stacktrace points to the line

u[end-idxs:end] = 0.5

The issue here is that findall returns an array even when it only finds one element, eg

julia> findall(x -> x > 2, [1,2,3])
1-element Array{Int64,1}:
 3

and you can't subtract an array from end in your indexing expression.

I don't understand enough about your code to figure out what idxs should be, but if you expect this to only return one element you could either use first(idxs) (or even only(idxs) in Julia 1.4), or replace findall with findfirst , which returns the index as an Integer (rather than an array).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM