我有一个这样的数组
a = [ rand(1:20,3) for i in 1:2,j in 1:3]
2×3 Array{Array{Int64,1},2}:
2×3 Array{Array{Int64,1},2}:
[8, 13, 1] [12, 4, 9] [13, 18, 7]
[3, 19, 20] [8, 3, 11] [10, 9, 12]
我希望对于每一行,获得一个 1 x 3 的数组,沿行的每个位置都有最大值。例如,在上面的例子中:[13,18,9]
[10,19,20]
(这个问题也是在另一个平台问的:https://discourse.julialang.org/t/find-the-maximum-for-each-position-of-a-set-of-arrays/62155/3)
最佳答案
如果我理解您的问题,您可以对 maximum
进行矢量化处理:
julia> a = [ rand(1:20,3) for i in 1:2,j in 1:3]
2×3 Matrix{Vector{Int64}}:
[13, 10, 10] [14, 8, 9] [15, 8, 5]
[18, 18, 7] [19, 10, 17] [17, 8, 7]
julia> maximum.(a)
2×3 Matrix{Int64}:
13 14 15
18 19 17
如果您想要一个行向量,请使用 collect
结合 eachrow
:julia> collect(eachrow(maximum.(a)))
2-element Vector{SubArray{Int64, 1, Matrix{Int64}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}}:
[13, 14, 15]
[18, 19, 17]
关于arrays - 在 Julia 中,找到一组数组每个位置的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67780836/