# Get row slice both to Vector and Matrix

**URL:** https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691
**Category:** New to Julia
**Tags:** indexing, arrays
**Created:** [June 13, 2022, 5:14pm UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691 "2022-06-13T17:14:21Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)
#### Post date: [June 13, 2022, 5:14pm UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691/1 "2022-06-13T17:14:21Z")

</div>

Is there an official way that can get row slice both to Vector and Matrix. It would works like this:

```julia
vec_data = [1,2,3,4]
mat_data = [[1,2,3,4] [2,3,4,5]]

row_index = RowIndex(1:3)

vec_data[row_index]

Vector{Int}
1
2
3

mat_data[row_index]

Matrix{Int}
1 2
2 3
3 4

```

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [June 13, 2022, 5:32pm UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691/2 "2022-06-13T17:32:00Z")

</div>

> [@Brian1](#):
>
> ```julia
> vec_data = [1,2,3,4]
> mat_data = [[1,2,3,4] [2,3,4,5]
> 
> ```

```julia
julia> struct RowIndex
           range::AbstractUnitRange
       end

julia> row_index = RowIndex(1:3)
RowIndex(1:3)

julia> function Base.getindex(a::AbstractArray{T,N}, row_index::RowIndex) where {T,N}
           ind = ntuple(N) do i
               i == 1 ? row_index.range : Colon()
           end
           getindex(a, ind...)
       end

julia> vec_data[row_index]
3-element Vector{Int64}:
 1
 2
 3

julia> mat_data[row_index]
3×2 Matrix{Int64}:
 1 2
 2 3
 3 4

```

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [June 13, 2022, 5:36pm UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691/3 "2022-06-13T17:36:55Z")

</div>

The answer above works if you want a special object for this operation. However, this can be done just using Julia’s native indexing rules with no special objects:

```julia
vec_data = [1,2,3,4]
mat_data = [[1,2,3,4] [2,3,4,5]]
rows = 1:3
vec_data[rows,:]
mat_data[rows,:]

```

---

<div class="post-metadata">

### Author: ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)
#### Post date: [June 13, 2022, 5:37pm UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691/4 "2022-06-13T17:37:33Z")

</div>

Thanks for your help. The `RowIndex` works fine. But is there an official way in julia which is similar to the `RowIndex` ?

---

<div class="post-metadata">

### Author: ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)
#### Post date: [June 13, 2022, 5:39pm UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691/5 "2022-06-13T17:39:42Z")

</div>

> [@mikmoore](#):
>
> `vec_data[rows,:]`

This does not work, It return a Matrix rather than Vector. It is the mechanism that makes me headeach

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [June 13, 2022, 5:48pm UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691/6 "2022-06-13T17:48:26Z")

</div>

Does the remainder of your code not work for a `Matrix` in that position? There’s a good chance it will. If not, then you’ll need to write things differently or use the suggested `RowIndex` object from the earlier answer.

Akin to the `RowIndex` object above, but without defining or building it specifically, you could use this function:

```julia
getrows(x::AbstractVector,rows) = x[rows]
getrows(x::AbstractMatrix,rows) = x[rows,:]
# can write for higher dimensional arrays if needed
# or can write a single definition that works for any number of dimensions, which looks much like the `getindex` function for `RowIndex` defined above

```

---

<div class="post-metadata">

### Author: ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)
#### Post date: [June 13, 2022, 6:02pm UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691/7 "2022-06-13T18:02:04Z")

</div>

In the context, the code work for `Vetor` or `Matrix`.

Define a function is intuitive, but I would thought thers is a more elegant way like `CartesianIndex` or some way else that is created to do this.

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [June 13, 2022, 7:18pm UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691/8 "2022-06-13T19:18:45Z")

</div>

In terms of already available functions, there is also `selectdim(v, 1, idxs)`, which will select entries of the array where the first index varies within `idxs`. In your example,

```julia
julia> selectdim(vec_data, 1, 1:3)
3-element view(::Vector{Int64}, 1:3) with eltype Int64:
 1
 2
 3

julia> selectdim(mat_data, 1, 1:3)
3×2 view(::Matrix{Int64}, 1:3, :) with eltype Int64:
 1 2
 2 3
 3 4

```

This returns a view of the original array rather than copying the data, not sure which is best for your use case (you can always call `copy` on the output to get a copy of the data).

---

<div class="post-metadata">

### Author: ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)
#### Post date: [June 14, 2022, 12:43am UTC](https://discourse.julialang.org/t/get-row-slice-both-to-vector-and-matrix/82691/9 "2022-06-14T00:43:39Z")

</div>

Thanks for your help. This function `selectdim` do the exactly same thing as `RowIndex`.
