# How to perform an argmax / argmin on a subset of a vector?

**URL:** <https://discourse.julialang.org/t/how-to-perform-an-argmax-argmin-on-a-subset-of-a-vector/70569>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [October 28, 2021, 4:33pm UTC](https://discourse.julialang.org/t/how-to-perform-an-argmax-argmin-on-a-subset-of-a-vector/70569 "2021-10-28T16:33:36Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 30, 2021, 12:11am UTC](https://discourse.julialang.org/t/how-to-perform-an-argmax-argmin-on-a-subset-of-a-vector/70569/23 "2021-10-30T00:11:54Z")

</div>

oof, embarrasing error from my part. i misunderstood the meaning of the start argument on `findnext`.the function that should do what op wants is along the lines of:

```julia
function findmax_view(x,b)
    c = argmax(view(x,b))
    i0 = findfirst(identity,b) #find the first value
    for _ in 1:c-1 #findnext iteratively c-1 times 
        i0 = findnext(identity,b,i0+1)
    end
    return i0
end

```

---

<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:** [October 30, 2021, 8:03am UTC](https://discourse.julialang.org/t/how-to-perform-an-argmax-argmin-on-a-subset-of-a-vector/70569/24 "2021-10-30T08:03:16Z")

</div>

Proposing one more lamp to the store with a slight twist:

```julia
function argmax_parentindex(x,b)
    v = view(x,b)
    first(parentindices(v))[argmax(v)]
end

```

_ **NB:** updated benchmark plots in post further above_

[Previous page](https://discourse.julialang.org/t/how-to-perform-an-argmax-argmin-on-a-subset-of-a-vector/70569.md?page=1)
