# Indexing into multidimensional array for general n dimensions

**URL:** https://discourse.julialang.org/t/indexing-into-multidimensional-array-for-general-n-dimensions/52556
**Category:** New to Julia
**Tags:** question
**Created:** [December 29, 2020, 6:01am UTC](https://discourse.julialang.org/t/indexing-into-multidimensional-array-for-general-n-dimensions/52556 "2020-12-29T06:01:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![gxyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gxyd/32/19986_2.png) [@gxyd](https://discourse.julialang.org/u/gxyd)
#### Post date: [December 29, 2020, 6:01am UTC](https://discourse.julialang.org/t/indexing-into-multidimensional-array-for-general-n-dimensions/52556/1 "2020-12-29T06:01:38Z")

</div>

Hi,

Let’s consider an array `X` of two dimensions, here `X[1,1]` is of type `Array{Float64, 2}`, i.e. `n = 2`:

```julia
julia> X
2×2 Array{Array{Float64,2},2}:
 [0.0 0.0; 0.0 0.0; 0.0 0.0] [2.0 2.0; 2.0 2.0; 2.0 2.0]
 [2.0 2.0; 2.0 2.0; 2.0 2.0] [2.0 2.0; 2.0 2.0; 2.0 2.0]

```

I obtain `y` from `X` by doing:

```julia
julia> [y[i,j,:,:] = X[i,j] for i in 1:size(X, 1) for j in 1:size(X, 2)]

```

Now for the general case when `X[1][1]` is of type `Array{Float64, n}` where `n ∈ ℕ`, what can I do to get `y` for general `n` in similar way?

pseudo-code would be:

```julia
julia> [y[i,j,:,:,:,:,:,.....] = X[i,j] for i in 1:size(X, 1) for j in 1:size(X, 2)]

```

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [December 29, 2020, 7:51am UTC](https://discourse.julialang.org/t/indexing-into-multidimensional-array-for-general-n-dimensions/52556/2 "2020-12-29T07:51:51Z")

</div>

Maybe you want [https://github.com/ChrisRackauckas/EllipsisNotation.jl](https://github.com/ChrisRackauckas/EllipsisNotation.jl). And more generally [https://github.com/mcabbott/LazyStack.jl](https://github.com/mcabbott/LazyStack.jl) or one of the packages mentioned there.

---

<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: [December 29, 2020, 8:11am UTC](https://discourse.julialang.org/t/indexing-into-multidimensional-array-for-general-n-dimensions/52556/3 "2020-12-29T08:11:18Z")

</div>

> [@gxyd](#):
>
> ```julia
> julia> [y[i,j,:,:] = X[i,j] for i in 1:size(X, 1) for j in 1:size(X, 2)]
> 
> ```

Are you sure this is how you want the data organized? You are taking data that are originally sequentially located in memory and scattering them far from each other (this is the opposite of what would happen Python, btw, where the above pattern is optimal). If you want to keep them sequential in memory, it makes more sense to do

```julia
julia> [y[:,:, i, j] = X[i,j] for i in axes(X, 1) for j in axes(X, 2)]

```

(using `axes` is a bit more robust than `1:length`)
