# Assignment to Matrix

**URL:** <https://discourse.julialang.org/t/assignment-to-matrix/51413>\
**Category:** New to Julia\
**Created:** [December 7, 2020, 4:34pm UTC](https://discourse.julialang.org/t/assignment-to-matrix/51413 "2020-12-07T16:34:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [December 7, 2020, 4:34pm UTC](https://discourse.julialang.org/t/assignment-to-matrix/51413/1 "2020-12-07T16:34:38Z")

</div>

It seems that assignment to a matrix (as opposed to a tuple) is not supported, right ?

```julia
function foo()
    return [[[1,2,3],"abc",'e'] [1, 2, 3]]
end

[a b; c d; e f] = foo() #error

```

Is there any simple workaround (other than the obvious one to stick with tuples) ?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 7, 2020, 4:56pm UTC](https://discourse.julialang.org/t/assignment-to-matrix/51413/2 "2020-12-07T16:56:21Z")

</div>

> [@sylvaticus](#):
>
> ```julia
> function foo()
> return [[[1,2,3],"abc",'e'] [1, 2, 3]]
> end
> 
> ```

Uhm… I think you either want a new matrix or to replace the elements of a previously allocated matrix:

```julia
julia> function foo()
           return [[[1,2,3],"abc",'e'] [1, 2, 3]]
       end
foo (generic function with 1 method)

julia> a = foo()
3×2 Array{Any,2}:
 [1, 2, 3] 1
 "abc" 2
 'e' 3

julia> a = Matrix{Any}(undef,3,2) 
3×2 Array{Any,2}:
 #undef #undef
 #undef #undef
 #undef #undef

julia> a .= foo()
3×2 Array{Any,2}:
 [1, 2, 3] 1
 "abc" 2
 'e' 3

```

Or do you want six variables with names `a`, … `f` associated to the elements of the matrix that comes out from `foo()`?

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [December 7, 2020, 6:18pm UTC](https://discourse.julialang.org/t/assignment-to-matrix/51413/3 "2020-12-07T18:18:53Z")

</div>

Yeah it’s entirely non-obvious what the goal is. But if you mean some kind of “array destructuring” similar to how tuple destructuring works, then this should totally be possible but I haven’t seen an implementation of it yet. What already works is:

```julia
julia> (a,b,c,d) = [1 2 ; 3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

julia> a
1

julia> b
3

julia> c
2

julia> d
4

```

So all it’d take is some metaprogramming magic for a macro to flatten the left-hand side to a tuple in the same way that the right-hand side is flattened. (and maybe some shape checking)  
Too bad I’m bad at this, but you could try it yourself: [Metaprogramming · The Julia Language](https://docs.julialang.org/en/v1/manual/metaprogramming/)

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [December 7, 2020, 7:13pm UTC](https://discourse.julialang.org/t/assignment-to-matrix/51413/4 "2020-12-07T19:13:21Z")

</div>

I was meaning the second one (having the 6 variables associated to the elements of the matrix).

The idea was for a function like:

`[m1Part1 m1Part2 m1Part3; m2Part1 m2Part3 m2Part3] = partition([matrix1,matrix2],[0.7,0.2,0.1])`

where then I was interested only in the individual matrices (like xtrain, xvalidation, xtest…) rathen than in the whole matrix itself coming out from `partition`.

But I can stick with tuples, just having it in a matrix form would have been more elegant…
