# Constructing multi-dimensional array from existing ones

**URL:** <https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268>\
**Category:** New to Julia\
**Created:** [October 24, 2019, 1:09pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268 "2019-10-24T13:09:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![liwt31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwt31/32/10973_2.png) [@liwt31](https://discourse.julialang.org/u/liwt31)\
**Post date:** [October 24, 2019, 1:09pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/1 "2019-10-24T13:09:29Z")

</div>

Hi, I’m a new user from Python. I’m trying to do something like:

```nohighlight
a, b = np.random.rand(5,5), np.random.rand(5,5)
c = np.array([a, b])

```

to obtain an array with dimension (2, 5, 5). But the following code seems not working as expected:

```nohighlight
a, b = rand(5,5), rand(5,5)
c = Array([a, b])

```

In this way `c` seems to be an array with 2 elements with each element a (5, 5) array.

I know this is a silly question, but I can’t find any solution online, probably due to wrong keywords.  
I’m curious why this is the behavior in Julia and what is the correct way to achieve my goal. Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 24, 2019, 1:17pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/2 "2019-10-24T13:17:26Z")

</div>

You may be looking for

```julia
permutedims(cat(a, b; dims = 3), (3, 1, 2))

```

You can read

[https://docs.julialang.org/en/v1/manual/arrays/#Concatenation-1](https://docs.julialang.org/en/v1/manual/arrays/#Concatenation-1)

but the whole array chapter is worth reading.

---

<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:** [October 24, 2019, 1:28pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/3 "2019-10-24T13:28:46Z")

</div>

Imho it makes most sense to change your algorithm to concatenate along the last dimension. In that case just do like @Tamas_Papp suggested, except do

```julia
cat(a, b; dims = 3)

```

The way Julia works is very consistent: `[x, y]` creates a vector. The first element is `x` the second element is `y`. It doesn’t matter what `x` and `y` are. What you see is what you get.

On the other hand, I think it would be pretty hard to predict the behaviour of numpy: Shift the dimensions of `x` and `y` outwards, then concatenate along the first dimension, oh but only if `x` and `y` are numpy arrays. I find this pretty mind-bending.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 24, 2019, 1:39pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/4 "2019-10-24T13:39:45Z")

</div>

Indeed, doing just this (without `permutedims`) and then using a different axis order would be what most Julia programmers would do, [unless access patterns suggest something else](https://docs.julialang.org/en/latest/manual/performance-tips/#Access-arrays-in-memory-order,-along-columns-1).

---

<div class="post-metadata">

**Author:** ![liwt31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwt31/32/10973_2.png) [@liwt31](https://discourse.julialang.org/u/liwt31)\
**Post date:** [October 24, 2019, 1:43pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/5 "2019-10-24T13:43:27Z")

</div>

Thanks for your comments! So the behavior is a bit similar to Matlab then. Actually in my case the order of the indices does not matter much, so I’d prefer `cat(a, b; dims = 3)`. But I’ll choose @Tamas_Papp’s answer as the solution because it faithfully produces the behavior I asked in the original question. I think this little snippet is worth being adding to somewhere more visible considering a large fraction of new Julia users is coming from Python. Anyway, my problem is solved and many thanks to you all 😀

---

<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:** [October 24, 2019, 1:55pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/6 "2019-10-24T13:55:43Z")

</div>

> [@liwt31](#):
>
> So the behavior is a bit similar to Matlab then.

Actually, in Matlab you get a third behaviour:

```julia
>> a = rand(5,5); b = rand(5, 5);
>> size([a, b])
ans =
     5 10

```

It concatenates along the second dimension, without any dimension-moving, so you end up with a 2D array.

---

<div class="post-metadata">

**Author:** ![liwt31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwt31/32/10973_2.png) [@liwt31](https://discourse.julialang.org/u/liwt31)\
**Post date:** [October 24, 2019, 2:04pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/7 "2019-10-24T14:04:05Z")

</div>

Sure, but `cat` function can also be used for this purpose:

```julia
>>> a = rand(5, 5); b = rand(5, 5);
>>> size(cat(3, a, b))
ans =

   5 5 2

```

I believe the idea here is quite similar with that in Julia.

---

<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:** [October 24, 2019, 2:05pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/8 "2019-10-24T14:05:47Z")

</div>

Ah, yes, `cat` is similar in Matlab and Julia, while `[...]` is different.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 24, 2019, 2:14pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/9 "2019-10-24T14:14:10Z")

</div>

> [@liwt31](#):
>
> I think this little snippet is worth being adding to somewhere more visible considering a large fraction of new Julia users is coming from Python.

There is

[https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python-1](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python-1)

but translating all the different idioms from Python would probably be impossible, and even a 90% attempt would be much longer than the current manual.

It is best to just understand Julia as it is, not in relation to Python. And the manual is the best starting point for most people with prior experience in another language.

---

<div class="post-metadata">

**Author:** ![liwt31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liwt31/32/10973_2.png) [@liwt31](https://discourse.julialang.org/u/liwt31)\
**Post date:** [October 24, 2019, 2:24pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/10 "2019-10-24T14:24:40Z")

</div>

Actually I find these comparisons quite helpful 😊 . Maybe I’ll compose a PR on this topic when I have some time.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 24, 2019, 2:44pm UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/11 "2019-10-24T14:44:40Z")

</div>

These comparisons _are_ intended to be useful — that’s why they were written. They are just not a substitute to learning about Julia idioms, because they cannot be a comprehensive guide.

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [January 20, 2020, 1:57am UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/12 "2020-01-20T01:57:31Z")

</div>

I just came across the need to do this as well. It would be great if `cat` had some way to easily specify that you want a dimension inserted. in APL you can concatenate along the `0.5`th or `1.5`th dimension to add a dimension at the beginning, or between the 1st and 2nd, respectively. So translating to Julia it would be as if:

```julia
a, b = rand(3,4), rand(3,4)
size(cat(a, b, dims=0.5)) == (2, 3, 4)
size(cat(a, b, dims=1.5)) == (3, 2, 4)

```

It feels a little weird to use a floating-point value as a dimension there, but it is a nice compact syntax for doing this sort of thing.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 20, 2020, 6:34am UTC](https://discourse.julialang.org/t/constructing-multi-dimensional-array-from-existing-ones/30268/13 "2020-01-20T06:34:04Z")

</div>

> [@ssfrr](#):
>
> a little weird to use a floating-point value as a dimension there

I sure that there are a lot of reasonable options for the syntax, eg `dims = 0 => 5`, or using `nothing` for insertion, etc.
