繁体   English   中英

MethodError: 没有方法匹配 -(::Int64, ::Array{Int64,1})

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

我尝试在 Julia 文档中使用这个示例。 我的尝试是将细胞分成两部分,每部分的蛋白质含量都是一半。

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)

我收到错误: MethodError: no method matching -(::Int64, ::Array{Int64,1}) 我知道idxs = findall(x->x>=1-eps(eltype(u)),u) ,我尝试在 1 和 eps 之间加一个点,但这并没有解决它。 我正在使用 Julia 1.1.1。

运行您的代码,堆栈跟踪指向该行

u[end-idxs:end] = 0.5

这里的问题是findall返回一个数组,即使它只找到一个元素,例如

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

并且您不能从索引表达式中的end减去数组。

我对你的代码理解不够,无法弄清楚idxs应该是什么,但如果你希望它只返回一个元素,你可以使用first(idxs) (甚至在 Julia 1.4 中使用only(idxs) ),或者替换findall使用findfirst ,它将索引作为整数(而不是数组)返回。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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