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

**URL:** https://discourse.julialang.org/t/cannot-manipulate-matrices-of-tuples-in-function-scope-but-works-fine-in-the-repl/66085
**Category:** New to Julia
**Tags:** question
**Created:** [August 9, 2021, 5:54pm UTC](https://discourse.julialang.org/t/cannot-manipulate-matrices-of-tuples-in-function-scope-but-works-fine-in-the-repl/66085 "2021-08-09T17:54:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![talkington](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@talkington](https://discourse.julialang.org/u/talkington)
#### Post date: [August 9, 2021, 5:54pm UTC](https://discourse.julialang.org/t/cannot-manipulate-matrices-of-tuples-in-function-scope-but-works-fine-in-the-repl/66085/1 "2021-08-09T17:54:59Z")

</div>

ver: 1.6.0

Define a matrix of (Float64,Float64) tuples using the `map` operator:

```julia
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:

```julia
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
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:

```julia
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:

```julia
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?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [August 9, 2021, 6:36pm UTC](https://discourse.julialang.org/t/cannot-manipulate-matrices-of-tuples-in-function-scope-but-works-fine-in-the-repl/66085/2 "2021-08-09T18:36:52Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [August 9, 2021, 7:05pm UTC](https://discourse.julialang.org/t/cannot-manipulate-matrices-of-tuples-in-function-scope-but-works-fine-in-the-repl/66085/3 "2021-08-09T19:05:37Z")

</div>

Welcome!

> [@talkington](#):
>
> In fact, I don’t even appear to successfully enter the function. `println("entered function") ` never runs when executing the following code:

That’s pretty good evidence that you’re not running what you think you’re running. Like [rdeits](https://discourse.julialang.org/u/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)`.

---

<div class="post-metadata">

### Author: ![Erhan](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Erhan](https://discourse.julialang.org/u/Erhan)
#### Post date: [August 9, 2021, 10:43pm UTC](https://discourse.julialang.org/t/cannot-manipulate-matrices-of-tuples-in-function-scope-but-works-fine-in-the-repl/66085/4 "2021-08-09T22:43:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![talkington](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@talkington](https://discourse.julialang.org/u/talkington)
#### Post date: [August 18, 2021, 6:02pm UTC](https://discourse.julialang.org/t/cannot-manipulate-matrices-of-tuples-in-function-scope-but-works-fine-in-the-repl/66085/5 "2021-08-18T18:02:57Z")

</div>

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.
