# Find the maximum for each position of a set of arrays

**URL:** <https://discourse.julialang.org/t/find-the-maximum-for-each-position-of-a-set-of-arrays/62155>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [May 31, 2021, 9:58pm UTC](https://discourse.julialang.org/t/find-the-maximum-for-each-position-of-a-set-of-arrays/62155 "2021-05-31T21:58:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [May 31, 2021, 9:58pm UTC](https://discourse.julialang.org/t/find-the-maximum-for-each-position-of-a-set-of-arrays/62155/1 "2021-05-31T21:58:01Z")

</div>

I have an array like this

```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]

```

I would like that for each row, obtain an array 1 x 3 with maximum in each position along the row. For example, in the example above:

```julia
[13,18,9]
[10,19,20]

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 31, 2021, 10:04pm UTC](https://discourse.julialang.org/t/find-the-maximum-for-each-position-of-a-set-of-arrays/62155/2 "2021-05-31T22:04:59Z")

</div>

`[maximum.(b) for b in eachrow(a)]`

---

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [May 31, 2021, 10:20pm UTC](https://discourse.julialang.org/t/find-the-maximum-for-each-position-of-a-set-of-arrays/62155/3 "2021-05-31T22:20:59Z")

</div>

It gives me the maximum of each elemt. I would like the maximimum by position, for each row.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [May 31, 2021, 10:36pm UTC](https://discourse.julialang.org/t/find-the-maximum-for-each-position-of-a-set-of-arrays/62155/4 "2021-05-31T22:36:35Z")

</div>

Here are two ways:

```julia
julia> a = [[[8, 13, 1]] [[12, 4, 9]] [[13, 18, 7]]
        [[3, 19, 20]] [[8, 3, 11]] [[10, 9, 12]] ] # as in original question
2×3 Matrix{Vector{Int64}}:
 [8, 13, 1] [12, 4, 9] [13, 18, 7]
 [3, 19, 20] [8, 3, 11] [10, 9, 12]

julia> [maximum.(b) for b in eachrow(a)] # answer above
2-element Vector{Vector{Int64}}:
 [13, 12, 18]
 [20, 11, 12]

julia> map(eachrow(a)) do row # desired result
         max.(row...)
       end
2-element Vector{Vector{Int64}}:
 [13, 18, 9]
 [10, 19, 20]

julia> using TensorCast

julia> @reduce _[i][k] := maximum(j) a[i,j][k]
2-element Vector{SubArray{Int64, 1, Matrix{Int64}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
 [13, 18, 9]
 [10, 19, 20]

```

But note that it’s normally easier to work with one 3D-array, instead of arrays of arrays. (This is what `@reduce` is doing, it concatenates, takes `maximum(tmp, dims = 3)`, then slices again.)

---

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [May 31, 2021, 10:42pm UTC](https://discourse.julialang.org/t/find-the-maximum-for-each-position-of-a-set-of-arrays/62155/5 "2021-05-31T22:42:03Z")

</div>

Thanks for the answer. If you wish, you can also answer the same question on the stackoverflow:  
[In Julia, Find the maximum for each position of a set of arrays - Stack Overflow](https://stackoverflow.com/questions/67780836/in-julia-find-the-maximum-for-each-position-of-a-set-of-arrays)  
I’ll give you the ok on this platform too.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 31, 2021, 10:50pm UTC](https://discourse.julialang.org/t/find-the-maximum-for-each-position-of-a-set-of-arrays/62155/6 "2021-05-31T22:50:57Z")

</div>

Ok, this one-liner comprehension seems to do the job:  
`[maximum(hcat(b...),dims=2) for b in eachrow(a)]`
