Skip to content
Related Articles

Related Articles

Improve Article

Iterating over each index of array in Julia – eachindex() Method

  • Last Updated : 26 Mar, 2020
Geek Week

The eachindex() is an inbuilt function in julia which is used to create an iterable object for visiting each index of the specified array.

Syntax:
eachindex(A…)

Parameters:

  • A: Specified array.

Returns: It returns an iterable object for visiting each index of the specified array.

Example 1:




# Julia program to illustrate 
# the use of Array eachindex() method
  
# Accessing each index of 1D array 
A = [1, 2, 3, 4];
  
# linear indexing
for i in eachindex(A)
    println(i)
end
  
# Accessing each index of 2D array 
B = [2 4; 6 8];
  
# linear indexing
for i in eachindex(B)
    println(i)
end
  
# Accessing each index of 3D array 
C = cat([1 2; 3 4], [5 6; 7 8], [2 2; 3 4], dims = 3);
  
# linear indexing
for i in eachindex(C)
    println(i)
end

Output:

 
Example 2:




# Julia program to illustrate 
# the use of Array eachindex() method
  
# Accessing each index of 1D array 
A = [1, 2, 3, 4];
  
# Cartesian indexing
for i in eachindex(view(A, 1:2, 1:1)) 
    println(i)
end
  
# Accessing each index of 2D array 
B = [2 4; 6 8];
  
# Cartesian indexing
for i in eachindex(view(B, :, 1)) 
    println(i)
end
  
# Accessing each index of 3D array 
C = cat([1 2; 3 4], [5 6; 7 8], [2 2; 3 4], dims = 3);
  
# Cartesian indexing
for i in eachindex(view(C, :, :, 1)) 
    println(i)
end

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :