slice
.. slice(A, inds...)
Returns a view of array ``A`` with the given indices like :func:`sub`, but drops all dimensions indexed with scalars.
Examples
The slice
function in Julia returns a view of the array A
with the given indices, similar to the sub
function. However, it drops all dimensions indexed with scalars.
julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
julia> slice(A, 1:2, 2:3)
2×2 Array{Int64,2}:
2 3
5 6
In this example, the slice
function is used to create a view of array A
using the indices 1:2
for rows and 2:3
for columns. The resulting view is a 2x2 array containing the selected elements.
Note that the slice
function drops any dimensions indexed with scalars. This means that if a single index is provided for a particular dimension, that dimension is removed from the resulting view.
julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
julia> slice(A, 2)
1-element view(::Array{Int64,2}, :, 2) with eltype Int64:
4
5
6
In this example, the slice
function is used with a single index 2
. The resulting view is a 3-element array representing the second column of the original array A
.
It's important to note that the slice
function returns a view of the original array, not a new array. Any modifications made to the view will affect the original array as well.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.