gpt4 book ai didi

multidimensional-array - Julia:跨矩阵行广播 `findfirst()`

转载 作者:行者123 更新时间:2023-12-02 02:45:01 27 4
gpt4 key购买 nike

我想找到满足某些条件的矩阵每行中第一个值的索引。我想弄清楚如何在不使用数组理解的情况下做到这一点。

这就是我使用数组理解的方式:

# 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)];

生成一个索引列表,告诉您每行(按列)的值首先从左到右下降到阈值以下。

您能想到更快的方法吗?

最佳答案

具体操作方法如下:

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

请注意,如果没有索引满足条件,findfirst 不会返回任何内容

关于multidimensional-array - Julia:跨矩阵行广播 `findfirst()`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62952775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com