# Find the index of minimum in a vector of vectors of vectors

**URL:** <https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571>\
**Category:** New to Julia\
**Created:** [March 5, 2023, 8:27am UTC](https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571 "2023-03-05T08:27:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [March 5, 2023, 8:27am UTC](https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571/1 "2023-03-05T08:27:47Z")

</div>

I think my problem is fairly simple.

```julia
a = [[1,2], [3,4,5], 10]
b = [[2,3], [4,5,6], 1] 
c = [a,b]

```

I want to find the index where the last element is the smallest. In the example, the value is 1, and it should return 2.

I tried to use the “findmin()”, but I simply didn’t know how to access the last element of each row.

Is only “for” possible to achieve this?

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [March 5, 2023, 8:34am UTC](https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571/2 "2023-03-05T08:34:02Z")

</div>

Can’t you use the keyword `by=last` ? Check the documentation of `findmin` for the correct syntax.

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [March 5, 2023, 8:36am UTC](https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571/3 "2023-03-05T08:36:21Z")

</div>

Thank you very much for your reply. What if I wanted to collect only the last elements from “c”?

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [March 5, 2023, 8:44am UTC](https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571/4 "2023-03-05T08:44:34Z")

</div>

You request is unclear. There is also `argmin` that you may be able to use.

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [March 5, 2023, 9:00am UTC](https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571/5 "2023-03-05T09:00:56Z")

</div>

The way I’ve thought is as follows

```julia
# collect only the last element of each row of the vector c. 

collection = Any[]

for (i,element) in enumerate(c)
push!(collection,element)
end

# find the index where the last element is the smallest 
ind = findmin(collection)

# return the row of c that contains the smallest last element
c[ind]

```

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [March 5, 2023, 9:11am UTC](https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571/6 "2023-03-05T09:11:42Z")

</div>

Try `findmin(c,by=last)`

You may also try broadcasting as `collection=last.(c)`

---

<div class="post-metadata">

**Author:** ![numberMX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/numbermx/32/31874_2.png) [@numberMX](https://discourse.julialang.org/u/numberMX)\
**Post date:** [March 5, 2023, 9:51am UTC](https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571/7 "2023-03-05T09:51:19Z")

</div>

`findmin` doesn’t seem to support a `by` keyword. The syntax for that seems to be `findmin(last, c)`.

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [March 5, 2023, 10:00am UTC](https://discourse.julialang.org/t/find-the-index-of-minimum-in-a-vector-of-vectors-of-vectors/95571/8 "2023-03-05T10:00:04Z")

</div>

Just in case someone finds this thread helpful, I tried both.  
Thank you so much everyone for the suggestions!

```julia
a = [[1,2], [3,4,5], 10]

b = [[2,3], [4,5,6], 1]

c = [a,b]

findmin(last,c)

(1, 2)

```

```julia
a = [1, [1,2], [3,4,5], 10]
b = [2, [3,3], [1,1,1], 20]

c= [a,b]

findmin(c,by=last)

MethodError: no method matching findmin(::Vector{Vector{Any}}; by=last)
Closest candidates are:
  findmin(::AbstractArray; dims) at reducedim.jl:1112 got unsupported keyword argument "by"
  findmin(::Any) at reduce.jl:977 got unsupported keyword argument "by"
  findmin(::Any, !Matched::SparseArrays.AbstractSparseVector{T}) where T at /Applications/Julia-1.8.5.app/Contents/Resources/julia/share/julia/stdlib/v1.8/SparseArrays/src/sparsevector.jl:1432 got unsupported keyword argument "by"
  ...

Stacktrace:
 [1] kwerr(::NamedTuple{(:by,), Tuple{typeof(last)}}, ::Function, ::Vector{Vector{Any}})
   @ Base ./error.jl:165
 [2] top-level scope
   @ ~/Documents/Migration/Julia Code/Quantitative (simple)/W SMM BF (March 4).ipynb:6

```
