# Pick rows from array (ndims not known in advance)

**URL:** <https://discourse.julialang.org/t/pick-rows-from-array-ndims-not-known-in-advance/59033>\
**Category:** New to Julia\
**Created:** [April 11, 2021, 1:07pm UTC](https://discourse.julialang.org/t/pick-rows-from-array-ndims-not-known-in-advance/59033 "2021-04-11T13:07:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [April 11, 2021, 1:07pm UTC](https://discourse.julialang.org/t/pick-rows-from-array-ndims-not-known-in-advance/59033/1 "2021-04-11T13:07:46Z")

</div>

I want to pick out some rows (in the vector `vv`, see below) from an array `x`. The `ndims(x)` is determined at run time.

So far, I use the approach illustrated below. Is there a better way?

```julia
x1 = [11 12 13;
      21 22 23;
      31 32 33]
x = cat(x1,x1.+100,dims=3) #my array, happens to be a 3D array this time

vv = [1,3] #rows to pick out

vc = Any[Colon() for i=1:ndims(x)]
vc[1] = vv #the rows to pick out
y = x[vc...] #the result

```

---

<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:** [April 11, 2021, 1:23pm UTC](https://discourse.julialang.org/t/pick-rows-from-array-ndims-not-known-in-advance/59033/2 "2021-04-11T13:23:30Z")

</div>

Two ways:

```julia
julia> yv = selectdim(x, 1, vv)
2×3×2 view(::Array{Int64, 3}, [1, 3], :, :) with eltype Int64:
[:, :, 1] =
 11 12 13
 31 32 33

[:, :, 2] =
 111 112 113
 131 132 133

julia> using EllipsisNotation

julia> yv == x[vv, ..]
true

```

---

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [April 11, 2021, 1:32pm UTC](https://discourse.julialang.org/t/pick-rows-from-array-ndims-not-known-in-advance/59033/4 "2021-04-11T13:32:08Z")

</div>

There is a `Base` function `selectdim` which I think does precisely that.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [April 11, 2021, 3:48pm UTC](https://discourse.julialang.org/t/pick-rows-from-array-ndims-not-known-in-advance/59033/5 "2021-04-11T15:48:21Z")

</div>

Thanks. This works well.

I (wrongly) believed that `selectdim(A, d::Integer, i)` required `i` to be a single index.

The documentation says  
`Return a view of all the data of A where the index for dimension d equals i.`

I guess that could be interpreted either way. In case I (or someone else here) come up with a better formulation, I would be happy to make a PR.

---

<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:** [April 11, 2021, 4:14pm UTC](https://discourse.julialang.org/t/pick-rows-from-array-ndims-not-known-in-advance/59033/6 "2021-04-11T16:14:47Z")

</div>

Maybe something as simple as adding `selectdim(A, 2, 3:4)` as another example, below the `selectdim(A, 2, 3)` it has now?

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [April 13, 2021, 8:38am UTC](https://discourse.julialang.org/t/pick-rows-from-array-ndims-not-known-in-advance/59033/7 "2021-04-13T08:38:06Z")

</div>

Thanks for the suggestion, It’s now done: [https://github.com/JuliaLang/julia/pull/40456](https://github.com/JuliaLang/julia/pull/40456)
