# How to stores arrays in matrices

**URL:** <https://discourse.julialang.org/t/how-to-stores-arrays-in-matrices/42428>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 2, 2020, 11:24am UTC](https://discourse.julialang.org/t/how-to-stores-arrays-in-matrices/42428 "2020-07-02T11:24:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BaronvonderHeyden](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@BaronvonderHeyden](https://discourse.julialang.org/u/BaronvonderHeyden)\
**Post date:** [July 2, 2020, 11:24am UTC](https://discourse.julialang.org/t/how-to-stores-arrays-in-matrices/42428/1 "2020-07-02T11:24:32Z")

</div>

Hey guys,

I have a question regarding arrays and matrices:

I would like to create a 2D matrix where the first column contains arrays and the second column contains integer values, e.g.

_Column 1_ ----- _Column 2_  
[“a” “b” “c”] -------- 1  
[“d” “e” “f”] -------- 2  
[“g” “h”] ------------ 4  
[“i” “j” “k” “lm”] ---- 9  
…

Unfortunately, when I try do this my array elements are “unpacked” and instead of storing the array as a whole, the individual elements are stored in the matrix.

For example:

`a = [["a" "b" "c"] 2]`

gives

```julia
1×4 Array{Any,2}:
 "a" "b" "c" 2

```

instead of

```julia
1×2 Array{Any,2}:
 ["a" "b" "c"] 2

```

Now, I have found a workaround for this by saying

```julia
a = Any[0 2]
a[1] = ["a" "b" "c"]

```

But surely that cannot be the correct/clean way of doing that. So, how do I do this correctly?

PS: This is my first question ever, if I posted in the wrong category or something else please tell me how do it correctly. Thanks.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 2, 2020, 11:37am UTC](https://discourse.julialang.org/t/how-to-stores-arrays-in-matrices/42428/2 "2020-07-02T11:37:24Z")

</div>

This is a bit of an unusual matrix, so I cannot find a nice way of constructing it with literals (not sure if it’s possible or not), but I would do this, which is pretty readable:

```julia
str = [["a", "b", "c"],
       ["d", "e", "f"],
       ["g", "h"],
       ["i", "j", "k", "lm"]]

num = [1, 2, 4, 9]

a = [str num]

```

As I said, this is a slightly odd matrix, are you sure that a matrix is the right data structure for what you are doing? Julia has a lot of nice data structures. If you are used to only using arrays, then it’s easy to overlook them.

Edit: Here’s a one-liner:

```julia
a = [[["a", "b", "c"], ["d", "e", "f"], ["g", "h"], ["i", "j", "k", "lm"]] [1, 2, 4, 9]]

```

Less readable, imho.

---

<div class="post-metadata">

**Author:** ![BaronvonderHeyden](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@BaronvonderHeyden](https://discourse.julialang.org/u/BaronvonderHeyden)\
**Post date:** [July 2, 2020, 2:01pm UTC](https://discourse.julialang.org/t/how-to-stores-arrays-in-matrices/42428/3 "2020-07-02T14:01:23Z")

</div>

Thank you very much!

So, I guess the solution simply is to put double brackets around my array, i.e. [[“a” “b” “c”]].

Because this:  
`a = [[["a" "b" "c"]] 3]`  
then works exactly as I want it to, giving me:

```julia
1×2 Array{Any,2}:
 ["a" "b" "c"] 3

```

But to be honest, I still dont really understand why that is necessary… It seems overcomplicated or even wrong. I mean intuitively I would think that [[“x”]] gives me “x” in an array in an array…

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 2, 2020, 2:13pm UTC](https://discourse.julialang.org/t/how-to-stores-arrays-in-matrices/42428/4 "2020-07-02T14:13:17Z")

</div>

> [@BaronvonderHeyden](#):
>
> `[[["a" "b" "c"]] 3]`

The meaning of `[x y]` is _concatenate x and y horizontally_. You can see it like this

```julia
julia> [[1 2 3] [4 5]]
1×5 Array{Int64,2}:
 1 2 3 4 5

```

and it works similarly with a scalar:

```julia
julia> [[1 2 3] 4]
1×4 Array{Int64,2}:
 1 2 3 4

```

Then it logically follows that to do what you want you have to write

```julia
julia> [[[1 2 3]] 4]
1×2 Array{Any,2}:
 [1 2 3] 4

```

because concatenation will unpack the arrays to ‘fuse’ them together, so you have to pack it one extra layer deep.

> [@BaronvonderHeyden](#):
>
> It seems overcomplicated or even wrong.

Maybe what you are trying to do is ‘wrong’? 😉 I mean, are you _sure_ you are using the right data structure for this?

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [July 3, 2020, 6:30am UTC](https://discourse.julialang.org/t/how-to-stores-arrays-in-matrices/42428/5 "2020-07-03T06:30:37Z")

</div>

> [@DNF](#):
>
> Maybe what you are trying to do is ‘wrong’? 😉 I mean, are you _sure_ you are using the right data structure for this?

To add to `@DNF`’s remark, if you tell us what you want to do, you may get a better suggestion for the data structure to use. Usually, instead of packing everything into an array, a struct with names is a better alternative.
