# Cannot manipulate matrices of tuples in function scope but works fine in the REPL

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?

1 Like

I can’t reproduce the error you’re seeing (I’m not using VSCode, but it’s very unlikely that that’s relevant).

Does the problem persist if you restart the VSCode julia terminal (or just restart VSCode entirely)? My best guess is that you have another function named `matrix` defined in that session which has a more specific argument type than the `matrix` function you’ve copied here, and therefore that other (broken? leftover?) `matrix` function is getting called instead.

4 Likes

Welcome!

That’s pretty good evidence that you’re not running what you think you’re running. Like rdeits, it works for me and I also suspect you have more than one method defined for that `matrix` function. You can see which one is being called by asking Julia: `@which matrix(min,max,n)`. And you can see all the defined methods with `methods(matrix)`.

5 Likes

It also works for me (apart from min and max being already defined as functions in Main, which is easily fixed.)

Hello all,

Thank you for your replies. Per @mbauman , the namespace in my VSCode environment was simply polluted after a long day of coding.

Cheers.

1 Like