Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: second largest element in a matrix
Replies: 32   Last Post: May 5, 2013 10:31 AM

 Search Thread: Advanced Search

 Messages: [ Previous | Next ]
 Pekka Posts: 520 Registered: 10/3/07
Re: second largest element in a matrix
Posted: Sep 19, 2008 1:55 AM
 Plain Text Reply

"Oluwa KuIse" <wespeakforex@yahoo.com> wrote in message <gavctp\$8tv\$1@fred.mathworks.com>...
> "Call SORT with two output arguments."
> Can you pls give me a quick example of how to do this? Thank you so much, Steve.
> "Steven Lord" <slord@mathworks.com> wrote in message <gavast\$1dd\$1@fred.mathworks.com>...

> >
> > "Oluwa KuIse" <wespeakforex@yahoo.com> wrote in message
> > news: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?

> >
> > Call SORT with two output arguments.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> >

Example for m by n matrix:
m=3;n=4;ggg = rand(m,n);
[svals,idx] = sort(ggg(:),'descend'); % sort to vector
svals(2) % second largest value
[II,JJ] = ind2sub([m,n],idx(2)) % position in the matrix

Date Subject Author
9/18/08 Oluwa KuIse
9/19/08 stephanie
9/19/08 Steven Lord
9/19/08 Oluwa KuIse
9/19/08 Pekka
9/19/08 Oluwa KuIse
9/21/08 Greg Heath
9/24/08 Walter Roberson
3/4/09 Justin
4/6/09 Bruno Luong
4/6/09 Jos
4/6/09 Bruno Luong
4/6/09 Bruno Luong
4/6/09 mike zander
4/7/09 Bruno Luong
4/7/09 Bruno Luong
4/3/09 Paul
11/7/12 sssbi2009@gmail.com
11/19/12 dpb
4/3/09 Paul
4/3/09 mike zander
4/3/09 mike zander
4/3/09 mike zander
4/3/09 Walter Roberson
4/3/09 mike zander
5/4/13 tilindg1@gmail.com
5/4/13 Nasser Abbasi
5/4/13 dpb
5/5/13 Nasser Abbasi
5/5/13 dpb
5/5/13 Bruno Luong
5/5/13 Nasser Abbasi
5/5/13 Bruno Luong

© The Math Forum at NCTM 1994-2018. All Rights Reserved.