dpb
Posts:
8,181
Registered:
6/7/07


Re: second largest element in a matrix
Posted:
Nov 19, 2012 12:47 PM


On 11/7/2012 2:04 PM, sssbi2009@gmail.com wrote: > On Friday, April 3, 2009 5:59:02 PM UTC5, Paul wrote: >> "Oluwa KuIse"<wespeakforex@yahoo.com> wrote in message<gav3p9$som$1@fred.mathworks.com>... > Hello,> Can anyone pls tell me how I can find the second (or third) > largest element in a matrix AND its position. The max function gives > the largest element and its position but how do I find the second > largest element and its position?
> if temp is your vector, you can get the second largest element as > follows: [c,i] = max(temp(temp~=max(temp))) > > why would you sort it? this > is the whole point of logical addressing in matlab. > > [c,i] = max(temp(temp~=max(temp))) > I like your answer. Could you show me how to write like this to fine the third max? Thanks
max(x(x<max(x(x~=max(x))))) % throws away ties
After a while it starts to get messy trying to keep straight the nesting levels... :)
And, at some number it would make sense to just go ahead and get the order vector. I've not tried any timings to see how many values and what size vectors is the breakpoint...


