# Way to slice an array of unknown number of dimensions?

**URL:** https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246
**Category:** New to Julia
**Tags:** question
**Created:** [August 6, 2019, 11:08pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246 "2019-08-06T23:08:10Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)
#### Post date: [August 6, 2019, 11:08pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/1 "2019-08-06T23:08:10Z")

</div>

This might be naive, but I couldn’t find an answer else where:

Let `A` be an Nd-array, where Nd is unknown exactly till runtime, e.g., only sure that `Nd>=3`.  
Is there a good way to slice it along, e.g., the `n<=3`th dimension?

For comparison, in numpy, one can do:

```python
>>> A = numpy.ones((3,3,3,3));
>>> A[1,:]
array([[[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],
       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],
       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]]])

```

This made things each for me, as the slice is shape-ready for broadcasting operations on `A` afterwards.

Thanks.

---

<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: [August 7, 2019, 5:21am UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/2 "2019-08-07T05:21:19Z")

</div>

> **[GitHub - bramtayl/JuliennedArrays.jl: Type stable array slicing](https://github.com/bramtayl/JuliennedArrays.jl/)**
>
> Type stable array slicing. Contribute to bramtayl/JuliennedArrays.jl development by creating an account on GitHub.

I think with constant folding, you could construct an argument to the API of this package that would be type stable.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [August 7, 2019, 5:59am UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/3 "2019-08-07T05:59:24Z")

</div>

There is also [EllipsisNotation.jl](https://github.com/ChrisRackauckas/EllipsisNotation.jl). Also see [https://github.com/JuliaLang/julia/issues/5405](https://github.com/JuliaLang/julia/issues/5405).

---

<div class="post-metadata">

### Author: ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)
#### Post date: [August 7, 2019, 2:03pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/4 "2019-08-07T14:03:48Z")

</div>

Thanks! I will check out those modules.

So I guess, for `≤ v1.1`, it’s yet a missing feature.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [August 7, 2019, 2:05pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/5 "2019-08-07T14:05:25Z")

</div>

More like `<= v1.3`.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [August 7, 2019, 2:56pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/6 "2019-08-07T14:56:37Z")

</div>

You can also just use `selectdim` for this. For your case

```julia
julia> A=ones(3,3,3,3);

julia> selectdim(A, 1, 1)
3×3×3 view(::Array{Float64,4}, 1, :, :, :) with eltype Float64:
[:, :, 1] =
 1.0 1.0 1.0
 1.0 1.0 1.0
 1.0 1.0 1.0

[:, :, 2] =
 1.0 1.0 1.0
 1.0 1.0 1.0
 1.0 1.0 1.0

[:, :, 3] =
 1.0 1.0 1.0
 1.0 1.0 1.0
 1.0 1.0 1.0

```

selects the first slice along the first dimension.

---

<div class="post-metadata">

### Author: ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)
#### Post date: [August 7, 2019, 3:07pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/7 "2019-08-07T15:07:22Z")

</div>

That actually works very well for me!  
Thanks!

* * *

A few more comments for future readers.

Played with `selectdim` for a little. `selectdim` is smooth for r-value, (retrieve).  
For l-value, (assign), one need `copyto!(selectdim(...), new_val)`, etc.

IMHO, it’s a bit more of a workaround, than the feature wanted, `A[1, ..]` (syntax sugar provided in [`EllipsisNotation`](https://github.com/ChrisRackauckas/EllipsisNotation.jl)).

* * *

See @simeonschaub 's reply for a better way of using `selectdim` as l-value. (not `copyto!`)

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [August 10, 2019, 12:33pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/8 "2019-08-10T12:33:47Z")

</div>

Have you tried `selectdim(A, 1, 1) .= new_val`?

---

<div class="post-metadata">

### Author: ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)
#### Post date: [August 10, 2019, 4:15pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/9 "2019-08-10T16:15:25Z")

</div>

ah, I see, thanks for that!  
That feels a bit tricky, I thought same sized array copy would go smooth through `=`.

---

<div class="post-metadata">

### Author: ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)
#### Post date: [August 12, 2019, 1:56pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/10 "2019-08-12T13:56:17Z")

</div>

I think it makes sense that you need `.=`. If you type `selectdim(A, 1, 1) = new_val` then Julia interprets this as a function definition, i.e., Julia thinks you are trying to make a function called `selectdim` with arguments `A`, `1`, and `1` that returns `new_val`. But `1` is an invalid argument name, hence the error `ERROR: syntax: "1" is not a valid function argument name`. (On the other hand, `selectdim(A, d, i) = new_val` would quietly create a function that accepts 3 arguments and always returns `new_val`.)

Additionally, `selectdim(...)` returns an object, and it doesn’t make sense to assign an object to a value, just as `1 = 2` doesn’t make sense.

---

<div class="post-metadata">

### Author: ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)
#### Post date: [August 12, 2019, 2:31pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/11 "2019-08-12T14:31:45Z")

</div>

Agreed, it just feels a bit extra mind work to me, especially when I had `p=selecdim(...)`, then, few lines later, having `p.=c`:  
“wait, why do I need an element-wise assignment here between two same sized variable…, oh…, it’s a `view` and `=` doesn’t go through”.  
Probably I am just not used to `view`, and expected pointer-like behavior from it…

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [August 12, 2019, 3:04pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/12 "2019-08-12T15:04:50Z")

</div>

The key isn’t the `view`, it’s the difference between `A = X`, `A[I] = X` and `A .= X`.

The first just creates a new name — an alias — for `X`. The second _updates_ an existing `A` at the given indices to contain `X`. The third _updates_ all indices of an existing `A` to contain `X`. All three of these forms are special syntaxes. This includes the second one — “indexed assignment” — so pulling out the indexing away from the `=` will mean something different.

You can use views in the latter two expressions to further restrict the indices. But you cannot use views in the first one because it’s not doing any updating at all!

---

<div class="post-metadata">

### Author: ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)
#### Post date: [August 12, 2019, 3:18pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/13 "2019-08-12T15:18:30Z")

</div>

Thanks a lot!  
It is very helpful knowing that `A=X` is creating an alias. I wish this clarification could be in the doc.

---

<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: [August 13, 2019, 6:34am UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/14 "2019-08-13T06:34:03Z")

</div>

As discussed multiple times here (search the forum), calling it an _alias_ is not helpful.

Generally, code like

```julia
A = X

```

simply makes `A` and `X` have the _same_ value, in the sense that `A === X`, ie (quoting from `?===`)

> in the sense that no program could distinguish them

This applies to all kinds of values: integers, strings, and arrays. For arrays, neither is an an alias of the other, they are the same. Consequently, for a function like

```julia
f(Z) = (Z[1] = 2; nothing)

```

both `f(A)` and `f(X)` after `A = X` should just modify the _same_ array, otherwise a program could distinguish them.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [August 13, 2019, 3:22pm UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/15 "2019-08-13T15:22:13Z")

</div>

> [@Tamas\_Papp](#):
>
> As discussed multiple times here (search the forum), calling it an _alias_ is not helpful.

It’s an alias in the sense of normal English language alias. Like how the FBI would use the word alias. For some folks that’s what makes it click. For others it does indeed just muddy the waters based upon how they’ve used “aliasing” in the context of computer languages. Perhaps when I use that word I should clarify it’s the FBI’s alias — just two names for the same thing.

> [@Tamas\_Papp](#):
>
> `A` and `X` have the _same_ value

This is where I find more confusion — I think it’s easier to say that `A` and `X` don’t _have_ or _hold_ values, they’re just names. That’s it.

As always with mental models and teaching, your mileage may vary.

---

<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: [August 14, 2019, 6:24am UTC](https://discourse.julialang.org/t/way-to-slice-an-array-of-unknown-number-of-dimensions/27246/16 "2019-08-14T06:24:45Z")

</div>

> [@mbauman](#):
>
> I think it’s easier to say that `A` and `X` don’t _have_ or _hold_ values, they’re just names.

If you prefer that usage, you can say that after `A = X`, `A` and `X` are variables that refer to the same value.

> [@mbauman](#):
>
> Like how the FBI would use the word alias. For some folks that’s what makes it click.

Sorry, since I don’t follow criminal news of the US I am not sure how the FBI uses aliases.

I just think the term is misleading if it suggests that one of those variables is “the original” or is distinguished in some sense. I find that this interpretation is quite common when people talk about aliases.
