arrays - 在 Julia 中,找到一组数组每个位置的最大值

标签 arrays julia

我有一个这样的数组

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/

相关文章:

ios - 从 coredata 创建 Swift 数组

javascript - 通过运算将字符串与数字分开

c++ - 将整数放入数组 C++

arrays - 矩阵 block 索引

julia - 带有附加参数的管道

arrays - 数组中的最后一个元素覆盖所有先前的元素

c# - JSON.Net无法在自定义JsonConverter中反序列化json数组

julia - `isbits` 类型参数的算术运算

arrays - 逐个元素构建 Julia 数组的有效方法

collections - Set 和 Array 之间是否存在 Collection 父类(super class)型?如果不是,一个函数如何在 Sets 和 Arrays 上是多态的(用于迭代)?