# Keep dimensionality of slices

**URL:** <https://discourse.julialang.org/t/keep-dimensionality-of-slices/119261>\
**Category:** General Usage\
**Created:** [September 10, 2024, 10:27pm UTC](https://discourse.julialang.org/t/keep-dimensionality-of-slices/119261 "2024-09-10T22:27:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 10, 2024, 10:27pm UTC](https://discourse.julialang.org/t/keep-dimensionality-of-slices/119261/1 "2024-09-10T22:27:08Z")

</div>

The slices are rows internally but they come out as columns.

```julia
julia> let x = [1 2 3 4
                5 6 7 8]    
           collect(first(eachslice(x; dims=1)))
       end
4-element Vector{Int64}:
 1
 2
 3
 4

```

How can I get each slice, retaining their (1,n) shape? (I’m interested in working on arbitrary-dimensional arrays, not just matrices.)

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [September 10, 2024, 11:07pm UTC](https://discourse.julialang.org/t/keep-dimensionality-of-slices/119261/2 "2024-09-10T23:07:39Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 10, 2024, 11:21pm UTC](https://discourse.julialang.org/t/keep-dimensionality-of-slices/119261/3 "2024-09-10T23:21:23Z")

</div>

To be more precise, I’m looking for an expression that

- works for any value of ndims generically. `x[1:1, :]` works great for matrices but not higher or lower dims
- produces an iterable collection of these slices, not just the first one

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [September 10, 2024, 11:48pm UTC](https://discourse.julialang.org/t/keep-dimensionality-of-slices/119261/5 "2024-09-10T23:48:52Z")

</div>

Sounds like you want the `selectdim` function:

```julia
julia> x = reshape(1:24, (2,3,4))
2×3×4 reshape(::UnitRange{Int64}, 2, 3, 4) with eltype Int64:
[:, :, 1] =
 1 3 5
 2 4 6

[:, :, 2] =
 7 9 11
 8 10 12

[:, :, 3] =
 13 15 17
 14 16 18

[:, :, 4] =
 19 21 23
 20 22 24

julia> y = (selectdim(x, 2, i:i) for i in axes(x,2))
Base.Generator{Base.OneTo{Int64}, var"#3#4"}(var"#3#4"(), Base.OneTo(3))

julia> first(y)
2×1×4 view(reshape(::UnitRange{Int64}, 2, 3, 4), :, 1:1, :) with eltype Int64:
[:, :, 1] =
 1
 2

[:, :, 2] =
 7
 8

[:, :, 3] =
 13
 14

[:, :, 4] =
 19
 20

julia> last(y)
2×1×4 view(reshape(::UnitRange{Int64}, 2, 3, 4), :, 3:3, :) with eltype Int64:
[:, :, 1] =
 5
 6

[:, :, 2] =
 11
 12

[:, :, 3] =
 17
 18

[:, :, 4] =
 23
 24

```

with the obvious generalization:

```julia
julia> sliceiter(x, m) = (selectdim(x, m, i:i) for i in axes(x,m))

```
