<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161492</link>
    <title>MATLAB Central Newsreader - The N largest values of an M x 1 matrix</title>
    <description>Feed for thread: The N largest values of an M x 1 matrix</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 03 Jan 2008 11:44:26 -0500</pubDate>
      <title>The N largest values of an M x 1 matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161492#407841</link>
      <author>Palle Uldenborg</author>
      <description>I would like to find the indexes of the N largest elements of an   M x&lt;br&gt;
1 matrix. right now I do something lik the following, but it doesn't&lt;br&gt;
scale well.&lt;br&gt;
&lt;br&gt;
N=10;M=100;a=rand(1,M);[val,ip]=sort(a);idx=ip(end-N:end);a(idx)&lt;br&gt;
&lt;br&gt;
Does matlab have a specialized function for this?&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Palle&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 03 Jan 2008 14:39:08 -0500</pubDate>
      <title>Re: The N largest values of an M x 1 matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161492#407869</link>
      <author>Jos </author>
      <description>Palle Uldenborg &amp;lt;palle.uldenborg@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;57537254-3f7e-43e4-8fc4-75d8de63b3d6@s8g2000prg.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; I would like to find the indexes of the N largest elements&lt;br&gt;
of an   M x&lt;br&gt;
&amp;gt; 1 matrix. right now I do something lik the following, but&lt;br&gt;
it doesn't&lt;br&gt;
&amp;gt; scale well.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;&lt;br&gt;
N=10;M=100;a=rand(1,M);[val,ip]=sort(a);idx=ip(end-N:end);a(idx)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Does matlab have a specialized function for this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;                            Palle&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
What do you mean by "it doesn't scale well"?&lt;br&gt;
&lt;br&gt;
Other approaches:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;val = sort(-a) ; % or sort(a,'descend')&lt;br&gt;
&amp;nbsp;&amp;nbsp;val(1:N) ;&lt;br&gt;
&lt;br&gt;
or&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;val = unique(-a) ;&lt;br&gt;
&amp;nbsp;&amp;nbsp;val(1:N)&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 03 Jan 2008 16:34:33 -0500</pubDate>
      <title>Re: The N largest values of an M x 1 matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161492#407888</link>
      <author>Palle Uldenborg</author>
      <description>On Jan 3, 3:39 pm, "Jos " &amp;lt;DEL...@jasenDEL.nl&amp;gt; wrote:&lt;br&gt;
&amp;gt; Palle Uldenborg &amp;lt;palle.uldenb...@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;gt; &amp;lt;57537254-3f7e-43e4-8fc4-75d8de63b...@s8g2000prg.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I would like to find the indexes of the N largest elements of an   M x&lt;br&gt;
&amp;gt; &amp;gt; 1 matrix. right now I do something lik the following, but&lt;br&gt;
&amp;gt; &amp;gt; it doesn't scale well.&lt;br&gt;
&amp;gt; &amp;gt; N=10;M=100;a=rand(1,M);[val,ip]=sort(a);idx=ip(end-N:end);a(idx)&lt;br&gt;
&amp;gt; &amp;gt; Does matlab have a specialized function for this?&lt;br&gt;
&amp;gt; What do you mean by "it doesn't scale well"?&lt;br&gt;
&lt;br&gt;
Thank you for your answer. Sorry if I was unclear.&lt;br&gt;
&lt;br&gt;
I meant that for for a fixed value of N the CPU cost does not scale&lt;br&gt;
linearly with M.&lt;br&gt;
&lt;br&gt;
I am interested in situations such as N=5 and M=100000, so I think&lt;br&gt;
that I could gain from a matlab function that did some kind of partial&lt;br&gt;
sorting.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Niels&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 03 Jan 2008 19:49:46 -0500</pubDate>
      <title>Re: The N largest values of an M x 1 matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161492#407917</link>
      <author>Jos </author>
      <description>Palle Uldenborg &amp;lt;palle.uldenborg@gmail.com&amp;gt; wrote in &lt;br&gt;
message &amp;lt;7222b5d8-b1b3-425c-be43-&lt;br&gt;
ac349257c5e7@e23g2000prf.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; On Jan 3, 3:39 pm, "Jos " &amp;lt;DEL...@jasenDEL.nl&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; Palle Uldenborg &amp;lt;palle.uldenb...@gmail.com&amp;gt; wrote in &lt;br&gt;
message&lt;br&gt;
&amp;gt; &amp;gt; &amp;lt;57537254-3f7e-43e4-8fc4-&lt;br&gt;
75d8de63b...@s8g2000prg.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; I would like to find the indexes of the N largest &lt;br&gt;
elements of an   M x&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; 1 matrix. right now I do something lik the following, &lt;br&gt;
but&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; it doesn't scale well.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; N=10;M=100;a=rand(1,M);[val,ip]=sort(a);idx=ip(end-&lt;br&gt;
N:end);a(idx)&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Does matlab have a specialized function for this?&lt;br&gt;
&amp;gt; &amp;gt; What do you mean by "it doesn't scale well"?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you for your answer. Sorry if I was unclear.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I meant that for for a fixed value of N the CPU cost does &lt;br&gt;
not scale&lt;br&gt;
&amp;gt; linearly with M.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I am interested in situations such as N=5 and M=100000, &lt;br&gt;
so I think&lt;br&gt;
&amp;gt; that I could gain from a matlab function that did some &lt;br&gt;
kind of partial&lt;br&gt;
&amp;gt; sorting.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;                        Niels&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
For N &amp;lt;&amp;lt; M, this may be faster then using sort&lt;br&gt;
&lt;br&gt;
M = 1000000 ;&lt;br&gt;
N = 5 ;&lt;br&gt;
A = rand(M,1) ;&lt;br&gt;
&lt;br&gt;
val(1:N) = NaN ;&lt;br&gt;
for j=1:N,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;[val(j),ind] = max(A) ;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;A(ind) = -Inf ;&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 03 Jan 2008 19:49:54 -0500</pubDate>
      <title>Re: The N largest values of an M x 1 matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161492#407918</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;7222b5d8-b1b3-425c-be43-ac349257c5e7@e23g2000prf.googlegroups.com&amp;gt;,&lt;br&gt;
Palle Uldenborg  &amp;lt;palle.uldenborg@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;&amp;gt; Palle Uldenborg &amp;lt;palle.uldenb...@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;gt;&amp;gt; &amp;lt;57537254-3f7e-43e4-8fc4-75d8de63b...@s8g2000prg.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt;&amp;gt; &amp;gt; I would like to find the indexes of the N largest elements of an   M x&lt;br&gt;
&amp;gt;&amp;gt; &amp;gt; 1 matrix. right now I do something lik the following, but&lt;br&gt;
&amp;gt;&amp;gt; &amp;gt; it doesn't scale well.&lt;br&gt;
&lt;br&gt;
&amp;gt;I meant that for for a fixed value of N the CPU cost does not scale&lt;br&gt;
&amp;gt;linearly with M.&lt;br&gt;
&lt;br&gt;
In that case, use max() to get the value and index of the first&lt;br&gt;
maximum, then replace that index position in the vector with NaN.&lt;br&gt;
Use max() to get the value and index on that vector to get the&lt;br&gt;
value and index of the second maximum, replace it with NaN,&lt;br&gt;
repeat until you have as many as you need. Each pass will be&lt;br&gt;
linear in M, so the total cost will be N*M, which beats&lt;br&gt;
M*log(M) that you would have for a sort().&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;"History is a pile of debris"                     -- Laurie Anderson&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 03 Jan 2008 20:49:41 -0500</pubDate>
      <title>Re: The N largest values of an M x 1 matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161492#407927</link>
      <author>Roger Stafford</author>
      <description>Palle Uldenborg &amp;lt;palle.uldenborg@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;7222b5d8-b1b3-425c-be43-&lt;br&gt;
ac349257c5e7@e23g2000prf.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Palle Uldenborg &amp;lt;palle.uldenb...@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;gt;......&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; I would like to find the indexes of the N largest elements of an   M x&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; 1 matrix.......&lt;br&gt;
&amp;gt; I am interested in situations such as N=5 and M=100000, so I think&lt;br&gt;
&amp;gt; that I could gain from a matlab function that did some kind of partial&lt;br&gt;
&amp;gt; sorting.&lt;br&gt;
&amp;gt;                        Niels&lt;br&gt;
---------&lt;br&gt;
&amp;nbsp;&amp;nbsp;For values as high as M = 100000 and as small as N = 5, it is true that a &lt;br&gt;
sort operation may be relatively inefficient, since it is an order M*log(M) &lt;br&gt;
algorithm.  I have not heard of a matlab "partial sort" function, so you would &lt;br&gt;
probably have to replace the sort with some kind of carefully thought out for-&lt;br&gt;
loop type algorithm.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;One strategy might be to construct an order M*N algorithm along the &lt;br&gt;
following lines using the max function.  Let a be the M x 1 vector.   Then do:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;q = M:-1:1; % Start with all of vector a in current list&lt;br&gt;
&amp;nbsp;p = zeros(N,1); % Allocate indices storage&lt;br&gt;
&amp;nbsp;for r = N:-1:1&lt;br&gt;
&amp;nbsp;&amp;nbsp;[ignore,s] = max(a(q)); % Find the current maximum&lt;br&gt;
&amp;nbsp;&amp;nbsp;p(r) = q(s); % Store its index&lt;br&gt;
&amp;nbsp;&amp;nbsp;q(s) = []; % Drop it from the current list&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
The N-element vector a(p) will have the N largest elements of a and p will &lt;br&gt;
have their indices.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;It is clear, however, that the number N would not have to be very large &lt;br&gt;
before this method would be far more inefficient than doing a straight sort.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;[Note: Between Jos and Roberson you probably have the essential equivalent &lt;br&gt;
of the above, but I'll include this anyhow.]&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 03 Jan 2008 21:11:41 -0500</pubDate>
      <title>Re: The N largest values of an M x 1 matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/161492#407932</link>
      <author>Ken Fleisher</author>
      <description>What about if there are 5 or more values that are equal and&lt;br&gt;
are the max? Are you looking for the top five "unique"&lt;br&gt;
values or simply the top five values (which could all be the&lt;br&gt;
same)?&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
