📌  相关文章
📜  从 Julia 中的给定数组索引中获取先前的真值 |数组 findprev() 方法

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

从 Julia 中的给定数组索引中获取先前的真值 |数组 findprev() 方法

findprev()是 julia 中的内置函数,用于返回指定数组 A 的真元素之前或包括 i 的前一个索引,如果未找到真值,则返回零。这里索引或键的值从 1 开始,即第一个元素的索引是 1,第二个元素的索引是 2,依此类推。

示例 1:

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

输出:

示例 2:

# Julia program to illustrate 
# the use of Array findprev() method
   
# Finding index of previous even value
# before or including index 3 from 
# the 1D array A
A = [1, 2, 5, 6]
println(findprev(iseven, A, 3))
   
# Finding index of previous even value
# before or including index (2, 2) from 
# the 2D array B of size 2 * 2
B = [3 5; 6 7]
println(findprev(iseven, B, 
        CartesianIndex(2, 2)))
   
# Finding index of previous even value 
# before or including index (2, 2, 1) 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(findprev(iseven, C, 
        CartesianIndex(2, 2, 1)))

输出: