Open In App

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

Last Updated : 26 Mar, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads