Path: news.mathworks.com!not-for-mail
From: "Jos " <DELjos@jasenDEL.nl>
Newsgroups: comp.soft-sys.matlab
Subject: Re: The N largest values of an M x 1 matrix
Date: Thu, 3 Jan 2008 14:39:08 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 31
Message-ID: <flis2c$4nu$1@fred.mathworks.com>
References: <57537254-3f7e-43e4-8fc4-75d8de63b3d6@s8g2000prg.googlegroups.com>
Reply-To: "Jos " <DELjos@jasenDEL.nl>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1199371148 4862 172.30.248.37 (3 Jan 2008 14:39:08 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 3 Jan 2008 14:39:08 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 870065
Xref: news.mathworks.com comp.soft-sys.matlab:444195


Palle Uldenborg <palle.uldenborg@gmail.com> wrote in message
<57537254-3f7e-43e4-8fc4-75d8de63b3d6@s8g2000prg.googlegroups.com>...
> I would like to find the indexes of the N largest elements
of an   M x
> 1 matrix. right now I do something lik the following, but
it doesn't
> scale well.
> 
>
N=10;M=100;a=rand(1,M);[val,ip]=sort(a);idx=ip(end-N:end);a(idx)
> 
> Does matlab have a specialized function for this?
> 
>                            Palle


What do you mean by "it doesn't scale well"?

Other approaches:

  val = sort(-a) ; % or sort(a,'descend')
  val(1:N) ;

or

  val = unique(-a) ;
  val(1:N)

hth
Jos