Get row slice both to Vector and Matrix

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> 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).

3 Likes