# Tuple of Arrays: assigning or accumulating into the first column of each tuple elements

**URL:** <https://discourse.julialang.org/t/tuple-of-arrays-assigning-or-accumulating-into-the-first-column-of-each-tuple-elements/30830>\
**Category:** New to Julia\
**Tags:** tuple\
**Created:** [November 7, 2019, 2:51pm UTC](https://discourse.julialang.org/t/tuple-of-arrays-assigning-or-accumulating-into-the-first-column-of-each-tuple-elements/30830 "2019-11-07T14:51:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [November 7, 2019, 2:51pm UTC](https://discourse.julialang.org/t/tuple-of-arrays-assigning-or-accumulating-into-the-first-column-of-each-tuple-elements/30830/1 "2019-11-07T14:51:41Z")

</div>

I’m trying to apply a matrix A to a single column of each element of X and accumulate into X, where X is a tuple of arrays.

```julia
N = 2
A = rand(N,N)
X = (ones(N,N),ones(N,N))
for fld in eachindex(X)
    X[fld][:,1] += A*X[fld][:,1]
end    

```

I can apply A\*X[fld][:,1] to all fields using map and broadcasted getindex, but I get an error if I try to set or accumulate into the columns of elements of the tuple

```julia
getindex.(X,(:),1) .+= map(x->A*x,getindex.(X,(:),1)) # doesn't work

```

Is there a way to do this without looping over the elements of the struct?

---

<div class="post-metadata">

**Author:** ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)\
**Post date:** [November 7, 2019, 3:14pm UTC](https://discourse.julialang.org/t/tuple-of-arrays-assigning-or-accumulating-into-the-first-column-of-each-tuple-elements/30830/2 "2019-11-07T15:14:27Z")

</div>

Hi Jesse,

I hope this is what you want. If you are writing in global scope and the code is performance sensitive then:

```julia
 f!(X,A)=X[:,1] += A*X[:,1]
f! (generic function with 2 methods)

f!.(X,Ref(A)) #note the dot

```

If not, then the simpler:

```julia
f!(X)=X[:,1] += A*X[:,1]
f!.(X)

```

Or maybe i misunderstood 🙂

Cheers!

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [November 7, 2019, 3:33pm UTC](https://discourse.julialang.org/t/tuple-of-arrays-assigning-or-accumulating-into-the-first-column-of-each-tuple-elements/30830/3 "2019-11-07T15:33:17Z")

</div>

Hi Rami - thanks! The evaluating of A\*X isn’t the issue, it’s storing it back into X which causes me problems. I’m trying to figure out how to do

```julia
X[1][:,1] = f!.(X)[1]
X[2][:,1] = f!.(X)[2]

```

without having to loop through the tuple elements.

---

<div class="post-metadata">

**Author:** ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)\
**Post date:** [November 7, 2019, 3:56pm UTC](https://discourse.julialang.org/t/tuple-of-arrays-assigning-or-accumulating-into-the-first-column-of-each-tuple-elements/30830/4 "2019-11-07T15:56:25Z")

</div>

The result **is** stored in `X`:

```julia
**julia>** A = rand(N,N)

2×2 Array{Float64,2}:

 0.96716 0.878143

 0.703457 0.99305 

**julia>** X = (ones(N,N),ones(N,N))

([1.0 1.0; 1.0 1.0], [1.0 1.0; 1.0 1.0])

**julia>** for fld in eachindex(X)

X[fld][:,1] += A*X[fld][:,1]

end  

**julia>** X

([2.8453 1.0; 2.69651 1.0], [2.8453 1.0; 2.69651 1.0])

**julia>** X = (ones(N,N),ones(N,N))

([1.0 1.0; 1.0 1.0], [1.0 1.0; 1.0 1.0])

**julia>** f!.(X)

([2.8453, 2.69651], [2.8453, 2.69651])

**julia>** X
([2.8453 1.0; 2.69651 1.0], [2.8453 1.0; 2.69651 1.0])

```

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [November 7, 2019, 5:22pm UTC](https://discourse.julialang.org/t/tuple-of-arrays-assigning-or-accumulating-into-the-first-column-of-each-tuple-elements/30830/5 "2019-11-07T17:22:25Z")

</div>

AH - my mistake, I missed that! Thanks, that works great.
