这个eachindex()
是 julia 中的一个内置函数,用于创建一个可迭代对象以访问指定数组的每个索引。
用法:
eachindex(A…)
参数:
- A:指定数组。
返回值:它返回一个可迭代对象,用于访问指定数组的每个索引。
范例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
输出:
范例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
输出:
相关用法
- Julia Array findprev()用法及代码示例
- Julia Array findnext()用法及代码示例
- Julia Array findfirst()用法及代码示例
- Julia Array findlast()用法及代码示例
- Julia isassigned()用法及代码示例
- Julia setindex!()用法及代码示例
- Julia findmax()用法及代码示例
- Julia findmin()用法及代码示例
- Julia getindex()用法及代码示例
- Julia foreach()用法及代码示例
- Julia Array fill()用法及代码示例
- Julia Array findall()用法及代码示例
- Julia Array reshape()用法及代码示例
- Julia parent()用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Iterating over each index of array in Julia – eachindex() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。