Reputation: 105
I have a problem with indexing when an array element is an argument to another array. It results in an "invalid index 1.0" error. For example:
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
Here is the code that you can copy to a REPL and find the error. What I get is an index error.
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])
Thank you
Upvotes: 0
Views: 416
Reputation: 2718
You need to cast the floating number from arr1
to use it as index. I modified the Minimum Working Example to work:
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])
Upvotes: 1