📌  相关文章
📜  获取 Julia 中所有具有真值的数组元素 |数组 findall() 方法

📅  最后修改于: 2022-05-13 01:55:29.417000             🧑  作者: Mango

获取 Julia 中所有具有真值的数组元素 |数组 findall() 方法

findall()是 julia 中的内置函数,用于返回指定数组 A 中所有真值的索引或键的向量。如果数组中不存在这样的真值,则返回一个空数组。这里索引或键的值从 1 开始,即第一个元素的索引是 1,第二个元素的索引是 2,依此类推。

示例 1:

# Julia program to illustrate 
# the use of Array findall() method
   
# Finding index of all true values from 
# the 1D array A
A = [false, true, true, false]
println(findall(A))
   
# Finding index of all true values from 
# the 2D array B of size 2 * 2
B = [false false; true false]
println(findall(B))
   
# Finding index of all true values 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(findall(C))

输出:

示例 2:

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

输出: