Related Articles

Related Articles

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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]))

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :