当数组元素是另一个数组的参数时,索引存在问题。这将导致“无效索引1.0”错误。例如:
i = 0
for l in 1:length
for s in 1:ser
for x in 1:s
i = i + 1
arr1[i] = x
end
end
end
for ts in 1:tiser
arr2[ts] = arr3[arr1[ts]]
end
这是您可以复制到REPL并查找错误的代码。我得到的是索引错误。
arr1 = Array{Float64,1}(1500)
arr2 = Array{Float64,1}(10000)
arr3 = Array{Float64,1}(10000)
for z in 1:100
arr3[z] = 1 + z
end
i = 0
for l in 1:100
for s in 1:5
for x in 1:s
i = i + 1
arr1[i] = x
end
end
end
for ts in 1:10000
arr2[ts] = arr3[arr1[ts]]
end
println(arr2[3])
谢谢
答案 0 :(得分:1)
您需要强制转换arr1
中的浮点数以将其用作索引。我修改了“最小工作示例”以使其工作:
arr1 = Array{Float64,1}(1500)
arr2 = Array{Float64,1}(10000)
arr3 = Array{Float64,1}(10000)
for z in 1:100
arr3[z] = 1 + z
end
i = 0
for l in 1:100
for s in 1:5
for x in 1:s
i = i + 1
arr1[i] = x
end
end
end
println(arr1[1:20])
for ts in 1:10000
from_arr1_as_index=Int(arr1[(ts-1)%1500+1])
arr2[ts] = arr3[from_arr1_as_index]
end
println(arr2[3])