📌  相关文章
📜  在 Julia 中获取数组的第一个真值的索引 |数组 findfirst() 方法

📅  最后修改于: 2021-11-25 04:42:29             🧑  作者: Mango

findfirst()是 julia 中的一个内置函数,用于返回指定数组中第一个真值的索引或键。这里索引或键的值从 1 开始,即第一个元素的索引为 1,第二个元素的索引为 2,依此类推。

示例 1:

# Julia program to illustrate 
# the use of Array findfirst() method
   
# Finding index of first true value from 
# the 1D array A
A = [false, true, true, false]
println(findfirst(A))
   
# Finding index of first true value from 
# the 2D array B of size 2 * 2
B = [false false; true false]
println(findfirst(B))
   
# Finding index of first true value from 
# the 3D array C of size 2 * 2*2
C = cat([false false; true false], 
        [false true; true false],
        [true false; true true], dims = 3)
println(findfirst(C))

输出:

示例 2:

# Julia program to illustrate 
# the use of Array findfirst() method
   
# Finding index of first even value from 
# the 1D array A
A = [1, 2, 5, 6]
println(findfirst(iseven, A))
   
# Finding index of first even value from 
# the 2D array B of size 2 * 2
B = [3 5; 6 7]
println(findfirst(iseven, B))
   
# Finding index of first even value from 
# the 3D array C of size 2 * 2*2
C = cat([1 2; 3 4], [5 6; 7 8], 
        [9 10; 11 12], dims = 3)
println(findfirst(iseven, C))

输出: