Creating a view of parent array in Julia – view(), @view and @views Methods
The view()
is an inbuilt function in julia which is used to return a view into the given parent array A with the given indices instead of making a copy.
Syntax: view(A, inds…)
Parameters:
- A: Specified parent array.
- inds: Specified indices.
Returns: It returns a view into the given parent array A with the given indices instead of making a copy.
Example:
# Julia program to illustrate # the use of view() method # Getting a view into the given parent # array A with the given indices # instead of making a copy. A = [ 5 , 10 , 15 , 20 ]; println(view(A, 2 )) B = [ 5 10 ; 15 20 ]; println(view(B, :, 1 )) C = cat([ 1 2 ; 3 4 ], [ 5 6 ; 7 8 ], dims = 3 ); println(view(C, :, :, 1 )) |
Output:
@view()
The @view()
is an inbuilt function in julia which is used to create a sub array from the given indexing expression.
Syntax:
@view A[inds…]Parameters:
- A: Specified parent array.
- inds: Specified indices.
Returns: It returns the created sub array from the given indexing expression.
Example:
# Julia program to illustrate # the use of @view() method # Getting the created sub array # from the given indexing expression. A = [ 5 , 10 , 15 , 20 ]; println(@view A[ 3 ]) B = [ 5 10 ; 15 20 ]; println(@view B[:, 1 ]) C = cat([ 1 2 ; 3 4 ], [ 5 6 ; 7 8 ], [ 2 2 ; 3 4 ], dims = 3 ); println(@view(C[:, :, 2 ])) |
Output:
@views()
The @views()
is an inbuilt function in julia which is used to convert every given array-slicing operation in the given expression.
Syntax:
@views expressionParameters:
- expression: Specified expression.
Returns: It returns the desired view.
Example:
# Julia program to illustrate # the use of @views() method # Getting the created sub array # from the given indexing expression. A = zeros( 4 , 4 ); @views for row in 2 : 4 b = A[row, :] b[:] . = row end println(A) |
Output: