【发布时间】:2020-05-20 12:41:06
【问题描述】:
这是我要使用的功能。我正在尝试使用一整周的温度数据和降水数据。这意味着参数:temp 和 precip,将是一个长度为 7 的数组。我该如何进行这项工作?
function humidityindex(temp, precip)
moist_effect = 0
temp_effect = 0
for i in 1:size(precip)
moist_effect += ((precip[i]/100) * (1-(i/10)))
temp_effect -= ((temp[i]/25) * (1-(i/10)))
end
effect = temp_effect + moist_effect
return effect
end
函数结果如下MethodError:
julia> t = rand(7); p = rand(7);
julia> humidityindex(t, p)
ERROR: MethodError: no method matching (::Colon)(::Int64, ::Tuple{Int64})
Closest candidates are:
Any(::T, ::Any, ::T) where T<:Real at range.jl:41
Any(::A, ::Any, ::C) where {A<:Real, C<:Real} at range.jl:10
Any(::T, ::Any, ::T) where T at range.jl:40
...
Stacktrace:
[1] humidityindex(::Array{Float64,1}, ::Array{Float64,1}) at ./REPL[1]:4
[2] top-level scope at REPL[3]:1
【问题讨论】:
-
你能解释一下你的问题到底是什么吗?乍一看,代码似乎没有任何问题,但当然这取决于
temp和precip是如何定义的。您能否将这些添加到您的示例中并显示该函数的输出与您的预期有何不同? -
感谢您的编辑 - 这正是 Fredrik 在下面预期的问题(
size()返回一个维度元组,因此您尝试创建一个范围1:(7, )而不是范围1:7)
标签: julia