# How to use minimum and maximum for a Vector{SVector{3}}

**URL:** <https://discourse.julialang.org/t/how-to-use-minimum-and-maximum-for-a-vector-svector-3/26235>\
**Category:** Performance\
**Tags:** question\
**Created:** [July 10, 2019, 7:43pm UTC](https://discourse.julialang.org/t/how-to-use-minimum-and-maximum-for-a-vector-svector-3/26235 "2019-07-10T19:43:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CFBaptista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfbaptista/32/9264_2.png) [@CFBaptista](https://discourse.julialang.org/u/CFBaptista)\
**Post date:** [July 10, 2019, 7:43pm UTC](https://discourse.julialang.org/t/how-to-use-minimum-and-maximum-for-a-vector-svector-3/26235/1 "2019-07-10T19:43:50Z")

</div>

If I want to determine the smallest values in the rows of `A::Matrix` it’s as easy as:

```
min = minimum(A, dims=2)

```

However, if I have a `Vector` of `SVector{3}`'s it’s a bit more complicated. This

```
using StaticArrays

function test()
    n = 10
    A = [SVector{3}(rand(3)) for i = 1:n]
    min = minimum(A)
    return min
end

min = test()

```

Will give me the element of `A` which has the smallest component instead of comparing all SVector{3}'s and return the smallest 1st, 2nd and 3rd components.

What actually gives me what I want is this:

```
function test2()
    n = 10
    A = [SVector{3}(rand(3)) for i = 1:n]
    minX = minimum(a[1] for a in A)
    minY = minimum(a[2] for a in A)
    minZ = minimum(a[3] for a in A)
    return SVector{3}(minX, minY, minZ)
end

min = test2()

```

Is there a more convenient way to do this without calling `minimum` 3 times? Other than a for loop that is, because `test2()` and a for-loop generalisation walks through `A` multiple times.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 10, 2019, 8:00pm UTC](https://discourse.julialang.org/t/how-to-use-minimum-and-maximum-for-a-vector-svector-3/26235/2 "2019-07-10T20:00:21Z")

</div>

> [@CFBaptista](#):
>
> Is there a more convenient way to do this without calling `minimum` 3 times?

`reduce((x,y) -> min.(x,y), A)` does what you want, I think.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [July 10, 2019, 11:19pm UTC](https://discourse.julialang.org/t/how-to-use-minimum-and-maximum-for-a-vector-svector-3/26235/3 "2019-07-10T23:19:42Z")

</div>

You can also get a view of the `SVector` vector as a matrix:

```julia
n = 10
A = [SVector{3}(rand(3)) for i = 1:n]
M = reshape(reinterpret(Float64,A),(3,:))
minimum(M,dims=2)

```

see [API · StaticArrays.jl](https://juliaarrays.github.io/StaticArrays.jl/dev/pages/api/#Arrays-of-static-arrays-1)

---

<div class="post-metadata">

**Author:** ![CFBaptista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfbaptista/32/9264_2.png) [@CFBaptista](https://discourse.julialang.org/u/CFBaptista)\
**Post date:** [July 11, 2019, 8:31am UTC](https://discourse.julialang.org/t/how-to-use-minimum-and-maximum-for-a-vector-svector-3/26235/4 "2019-07-11T08:31:24Z")

</div>

Yup, that does it! Many thanks 🙂

---

<div class="post-metadata">

**Author:** ![CFBaptista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfbaptista/32/9264_2.png) [@CFBaptista](https://discourse.julialang.org/u/CFBaptista)\
**Post date:** [July 11, 2019, 8:34am UTC](https://discourse.julialang.org/t/how-to-use-minimum-and-maximum-for-a-vector-svector-3/26235/5 "2019-07-11T08:34:46Z")

</div>

Thanks. I do prefer the conciseness of @stevengj proposal, however.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 11, 2019, 4:01pm UTC](https://discourse.julialang.org/t/how-to-use-minimum-and-maximum-for-a-vector-svector-3/26235/6 "2019-07-11T16:01:26Z")

</div>

In addition to being more concise, my `reduce((x,y) -> min.(x,y), A)` is also about 10× faster on my machine than using `reshape`, because the `reshape` technique discards the efficiency of StaticArrays for small-vector operations. It’s also about 3× faster than calling `minimum` three times, maybe because a single pass over the array has better spatial locality (better cache-line utilization).

---

<div class="post-metadata">

**Author:** ![CFBaptista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfbaptista/32/9264_2.png) [@CFBaptista](https://discourse.julialang.org/u/CFBaptista)\
**Post date:** [July 12, 2019, 2:04pm UTC](https://discourse.julialang.org/t/how-to-use-minimum-and-maximum-for-a-vector-svector-3/26235/7 "2019-07-12T14:04:21Z")

</div>

I had a gut feeling that it would be faster as well. Thanks for checking 🙂
