Con*_*nor 1 broadcast multidimensional-array logical-operators julia array-broadcasting
我想在满足某个条件的矩阵的每一行中找到第一个值的索引。我想弄清楚如何在不使用数组推导式的情况下做到这一点。
这就是我将如何使用数组理解来做到这一点:
# let's say we want to find the first column index, per row, where a number in that row is below some threshold.
threshold = 0.5;
data = randn(50,100);
first_threshold_crossings = [findfirst(data[i,:]<threshold) for i in 1:size(data,1)];
Run Code Online (Sandbox Code Playgroud)
生成一个索引列表,告诉您每行的位置(按列)从左到右首先下降到阈值以下的值。
你能想象这样做的任何更快的方法吗?
您可以这样做:
julia> using Random # For RNG reproducability
julia> A = rand(Random.MersenneTwister(0), 3, 3)
3×3 Array{Float64,2}:
0.823648 0.177329 0.0423017
0.910357 0.27888 0.0682693
0.164566 0.203477 0.361828
julia> findfirst.(x < 0.1, eachrow(A))
3-element Array{Union{Nothing, Int64},1}:
3
3
nothing
Run Code Online (Sandbox Code Playgroud)
请注意,如果没有索引满足条件,则findfirst返回nothing。