Creating a view of parent array in Julia – view(), @view and @views Methods
Last Updated :
21 Apr, 2020
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:
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:
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 expression
Parameters:
- expression: Specified expression.
Returns: It returns the desired view.
Example:
A = zeros( 4 , 4 );
@views for row in 2 : 4
b = A[row, :]
b[:] . = row
end
println(A)
|
Output:
Share your thoughts in the comments
Please Login to comment...