<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238315</link>
    <title>MATLAB Central Newsreader - Top N values in a multidimensional array</title>
    <description>Feed for thread: Top N values in a multidimensional array</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by 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>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 28 Oct 2008 19:43:02 -0400</pubDate>
      <title>Top N values in a multidimensional array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238315#607769</link>
      <author>David Doria</author>
      <description>I didn't see anything like on the file exchange, which really surprised me. &lt;br&gt;
&lt;br&gt;
I have a multidimensional array that I would like to query for the largest N values.  ie if i say&lt;br&gt;
&lt;br&gt;
[v loc] = max(A, 2);&lt;br&gt;
&lt;br&gt;
where A is 3x3x10, I would like v to be length 2 containing the largest two values, and loc to be length 2 containing the positions of those values (a coordinate in the nXmXp space).&lt;br&gt;
&lt;br&gt;
Does someone know of a script like this that I just missed on the file exchange? It just seems like it would be terribly inefficient if I wrote it.&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
Dave</description>
    </item>
    <item>
      <pubDate>Tue, 28 Oct 2008 20:28:02 -0400</pubDate>
      <title>Re: Top N values in a multidimensional array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238315#607777</link>
      <author>First Last</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message &amp;lt;ge7q06$4k2$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I didn't see anything like on the file exchange, which really surprised me. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a multidimensional array that I would like to query for the largest N values.  ie if i say&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [v loc] = max(A, 2);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; where A is 3x3x10, I would like v to be length 2 containing the largest two values, and loc to be length 2 containing the positions of those values (a coordinate in the nXmXp space).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Does someone know of a script like this that I just missed on the file exchange? It just seems like it would be terribly inefficient if I wrote it.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Dave&lt;br&gt;
&lt;br&gt;
Hi Dave,&lt;br&gt;
&lt;br&gt;
Just one of many solutions could look like (with A as you defined it):&lt;br&gt;
&lt;br&gt;
A_col = reshape(A,[],1);&lt;br&gt;
A_sort = sort(A_col,'descend');&lt;br&gt;
v = A_sort(1:2);&lt;br&gt;
lin_loc = find(A_col == v(1)); % for the first max&lt;br&gt;
real_loc = ind2sub(size(A),lin_loc); % for the first max&lt;br&gt;
&lt;br&gt;
Playing around with that will allow you to customize it. </description>
    </item>
    <item>
      <pubDate>Wed, 29 Oct 2008 13:20:03 -0400</pubDate>
      <title>Re: Top N values in a multidimensional array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238315#607895</link>
      <author>David Doria</author>
      <description>Hmm it seems like that returns a scalar when I would have liked a vector of the 3 coordinates - ie (4, 5, 2) where that just says 51.&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Dave</description>
    </item>
    <item>
      <pubDate>Wed, 29 Oct 2008 15:14:02 -0400</pubDate>
      <title>Re: Top N values in a multidimensional array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238315#607908</link>
      <author>David Doria</author>
      <description>I see, you actually have to give it 3 output values&lt;br&gt;
&lt;br&gt;
[i j k] = ind2sub();&lt;br&gt;
&lt;br&gt;
I thought it would just make 'output' a 1x3 vector with&lt;br&gt;
output = ind2sub();&lt;br&gt;
&lt;br&gt;
but I was wrong.&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Dave</description>
    </item>
    <item>
      <pubDate>Wed, 29 Oct 2008 18:46:03 -0400</pubDate>
      <title>Re: Top N values in a multidimensional array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238315#607941</link>
      <author>Wolfgang Schwanghart</author>
      <description>or just...&lt;br&gt;
&lt;br&gt;
n = 2; %number of max values&lt;br&gt;
&lt;br&gt;
[a,ix] = sort(A(:),'descend');&lt;br&gt;
a = a(1:n);&lt;br&gt;
ix = ix(1:n);&lt;br&gt;
&lt;br&gt;
than use ind2sub to find the subscripts.&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
Wolfgang</description>
    </item>
    <item>
      <pubDate>Thu, 18 Nov 2010 16:19:04 -0500</pubDate>
      <title>Re: Top N values in a multidimensional array</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238315#797392</link>
      <author>Natasha </author>
      <description>&quot;Wolfgang Schwanghart&quot; &amp;lt;schwanghart@googlemail.com&amp;gt; wrote in message &amp;lt;geab1b$1ad$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; or just...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; n = 2; %number of max values&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [a,ix] = sort(A(:),'descend');&lt;br&gt;
&amp;gt; a = a(1:n);&lt;br&gt;
&amp;gt; ix = ix(1:n);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; than use ind2sub to find the subscripts.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; HTH,&lt;br&gt;
&amp;gt; Wolfgang&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Thanks Wolfgang, that's a nice bit of code.&lt;br&gt;
Natasha</description>
    </item>
  </channel>
</rss>

