ver: 1.6.0
Define a matrix of (Float64,Float64) tuples using the map
operator:
using LinearAlgebra
n = 9
A = map(tuple, (0.0 for i=1:n,j=1:n),zeros(n,n))
Let’s say I want to iterate through the elements and change some entries conditionally:
min=0.0001
max=0.5
for i=1:n,j=1:n
if i==j
#println(i,j)
A[i,j] = (min,max)
end
end
Running the above code in the REPL works flawlessly with no issue:
julia> A
9×9 Matrix{Tuple{Float64, Float64}}:
(0.0001, 0.5) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0)
(0.0, 0.0) (0.0001, 0.5) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0)
(0.0, 0.0) (0.0, 0.0) (0.0001, 0.5) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0)
(0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0001, 0.5) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0)
(0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0001, 0.5) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0)
(0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0001, 0.5) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0)
(0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0001, 0.5) (0.0, 0.0) (0.0, 0.0)
(0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0001, 0.5) (0.0, 0.0)
(0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0, 0.0) (0.0001, 0.5)
Copy and pasting the exact same code into an function and running it in the VSCode IDE fails in a completely incomprehensible manner.
In fact, I don’t even appear to successfully enter the function. println("entered function")
never runs when executing the following code:
function matrix(min,max,n)
println("entered function")
A = map(tuple, (0.0 for i=1:n,j=1:n),zeros(n,n))
for i=1:n,j=1:n
if i==j
println(i,j)
A[i,j] = (min,max)
end
end
return A
end
matrix(min,max,n)
We get the following output:
ERROR:
LoadError: MethodError: no method matching setindex!(::Tuple{Float64, Float64}, ::Tuple{Float64, Float64}, ::Int64)
What on earth is going on here and how am I supposed to interpret this stacktrace?