<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154894</link>
    <title>MATLAB Central Newsreader - Array manipulation question</title>
    <description>Feed for thread: Array manipulation question</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, 21 Aug 2007 18:22:32 -0400</pubDate>
      <title>Array manipulation question</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154894#388620</link>
      <author>Adam </author>
      <description>Hi everyone,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;I'm looking for a way to have Matlab return some number &lt;br&gt;
(N) of the smallest elements in an array along with their &lt;br&gt;
indices in the array.  For example, given the array:&lt;br&gt;
&lt;br&gt;
[5 4 3 2 1] and N = 3&lt;br&gt;
&lt;br&gt;
The output should be something like:&lt;br&gt;
&lt;br&gt;
[[3 3],[2 4],[1 5]]&lt;br&gt;
&lt;br&gt;
This can be done with brute force but I'm looking for a &lt;br&gt;
cleaner implementation.  I can't use the sort function &lt;br&gt;
because it re-orders the array and I would lose the index &lt;br&gt;
information.  Does matlab support hash tables?  Any other &lt;br&gt;
ideas?  Thanks.&lt;br&gt;
&lt;br&gt;
Adam </description>
    </item>
    <item>
      <pubDate>Tue, 21 Aug 2007 18:36:56 -0400</pubDate>
      <title>Re: Array manipulation question</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154894#388622</link>
      <author>Peter Boettcher</author>
      <description>&quot;Adam &quot; &amp;lt;crossroads_evh.nospam@hotmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; Hi everyone,&lt;br&gt;
&amp;gt;    I'm looking for a way to have Matlab return some number &lt;br&gt;
&amp;gt; (N) of the smallest elements in an array along with their &lt;br&gt;
&amp;gt; indices in the array.  For example, given the array:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [5 4 3 2 1] and N = 3&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The output should be something like:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [[3 3],[2 4],[1 5]]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; This can be done with brute force but I'm looking for a &lt;br&gt;
&amp;gt; cleaner implementation.  I can't use the sort function &lt;br&gt;
&amp;gt; because it re-orders the array and I would lose the index &lt;br&gt;
&amp;gt; information.  Does matlab support hash tables?  Any other &lt;br&gt;
&amp;gt; ideas?  Thanks.&lt;br&gt;
&lt;br&gt;
help sort:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[Y,I] = SORT(X,DIM,MODE) also returns an index matrix I.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If X is a vector, then Y = X(I).  &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If X is an m-by-n matrix and DIM=1, then&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for j = 1:n, Y(:,j) = X(I(:,j),j); end&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&lt;br&gt;
-Peter</description>
    </item>
    <item>
      <pubDate>Tue, 21 Aug 2007 18:38:23 -0400</pubDate>
      <title>Re: Array manipulation question</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154894#388623</link>
      <author>us</author>
      <description>Adam:&lt;br&gt;
&amp;lt;SNIP error of &amp;lt;sort&amp;gt;s...&lt;br&gt;
&lt;br&gt;
&amp;gt; I can't use the sort function because it re-orders the &lt;br&gt;
array and I would lose the index information...&lt;br&gt;
&lt;br&gt;
why?&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;v=[3,4,1,2,5].';&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[vs,vx]=sort(v);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n=3;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r=[vs(1:n),vx(1:n)];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp(r);&lt;br&gt;
&lt;br&gt;
also, look at &amp;lt;loren shure&amp;gt;'s recent hootenanny of &amp;lt;sort&amp;gt;s&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://blogs.mathworks.com/loren/2007/08/21/reversal-of-a-&quot;&gt;http://blogs.mathworks.com/loren/2007/08/21/reversal-of-a-&lt;/a&gt;&lt;br&gt;
sort/&lt;br&gt;
&lt;br&gt;
to sort things out...&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Tue, 21 Aug 2007 18:45:15 -0400</pubDate>
      <title>Re: Array manipulation question</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154894#388625</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fafah8$ot$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Adam  &amp;lt;crossroads_evh.nospam@hotmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;   I'm looking for a way to have Matlab return some number &lt;br&gt;
&amp;gt;(N) of the smallest elements in an array along with their &lt;br&gt;
&amp;gt;indices in the array.  For example, given the array:&lt;br&gt;
&lt;br&gt;
&amp;gt;[5 4 3 2 1] and N = 3&lt;br&gt;
&lt;br&gt;
&amp;gt;The output should be something like:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;[[3 3],[2 4],[1 5]]&lt;br&gt;
&lt;br&gt;
&amp;gt;This can be done with brute force but I'm looking for a &lt;br&gt;
&amp;gt;cleaner implementation.  I can't use the sort function &lt;br&gt;
&amp;gt;because it re-orders the array and I would lose the index &lt;br&gt;
&amp;gt;information.&lt;br&gt;
&lt;br&gt;
A = [5 4 3 2 1];&lt;br&gt;
N = 3;&lt;br&gt;
&lt;br&gt;
[sortedvec, sortidx] = sort(A);&lt;br&gt;
origorder = sort(sortidx(1:N));&lt;br&gt;
result = [A(origorder);origorder]'&lt;br&gt;
&lt;br&gt;
result =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3     3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2     4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     5&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;There are some ideas so wrong that only a very intelligent person&lt;br&gt;
&amp;nbsp;&amp;nbsp;could believe in them.                              -- George Orwell</description>
    </item>
    <item>
      <pubDate>Tue, 21 Aug 2007 18:45:54 -0400</pubDate>
      <title>Re: Array manipulation question</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154894#388626</link>
      <author> &quot;J.N.</author>
      <description>On Aug 21, 2:22 pm, &quot;Adam &quot; &amp;lt;crossroads_evh.nos...@hotmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Hi everyone,&lt;br&gt;
&amp;gt;    I'm looking for a way to have Matlab return some number&lt;br&gt;
&amp;gt; (N) of the smallest elements in an array along with their&lt;br&gt;
&amp;gt; indices in the array.  For example, given the array:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [5 4 3 2 1] and N = 3&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The output should be something like:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [[3 3],[2 4],[1 5]]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; This can be done with brute force but I'm looking for a&lt;br&gt;
&amp;gt; cleaner implementation.  I can't use the sort function&lt;br&gt;
&amp;gt; because it re-orders the array and I would lose the index&lt;br&gt;
&amp;gt; information.  Does matlab support hash tables?  Any other&lt;br&gt;
&amp;gt; ideas?  Thanks.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Adam&lt;br&gt;
&lt;br&gt;
The syntax:&lt;br&gt;
[As,Ix] = sort(A)&lt;br&gt;
returns in Ix the indexes in A of the sorted elements in As. Picking&lt;br&gt;
the first N elements of the pair As and Ix should do what you want.&lt;br&gt;
&lt;br&gt;
J.N.</description>
    </item>
    <item>
      <pubDate>Tue, 21 Aug 2007 18:50:10 -0400</pubDate>
      <title>Re: Array manipulation question</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154894#388628</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fafbev$dku$1@fred.mathworks.com&amp;gt;, us  &amp;lt;us@neurol.unizh.ch&amp;gt; wrote:&lt;br&gt;
&amp;gt;Adam:&lt;br&gt;
&amp;gt;&amp;lt;SNIP error of &amp;lt;sort&amp;gt;s...&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; I can't use the sort function because it re-orders the &lt;br&gt;
&amp;gt;array and I would lose the index information...&lt;br&gt;
&lt;br&gt;
&amp;gt;     v=[3,4,1,2,5].';&lt;br&gt;
&amp;gt;     [vs,vx]=sort(v);&lt;br&gt;
&amp;gt;     n=3;&lt;br&gt;
&amp;gt;     r=[vs(1:n),vx(1:n)];&lt;br&gt;
&lt;br&gt;
That will give the values in sorted order, but Adam wants the values&lt;br&gt;
in the original order (for whatever reason). See my posting for&lt;br&gt;
the (small) change needed to get the original order.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;Okay, buzzwords only. Two syllables, tops.  -- Laurie Anderson</description>
    </item>
    <item>
      <pubDate>Tue, 21 Aug 2007 19:44:23 -0400</pubDate>
      <title>Re: Array manipulation question</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154894#388637</link>
      <author>Adam </author>
      <description>My mistake!  The sort function works fine. I'll have to &lt;br&gt;
read the help file more closely next time.  Thanks for the &lt;br&gt;
replies.</description>
    </item>
    <item>
      <pubDate>Tue, 21 Aug 2007 20:43:10 -0400</pubDate>
      <title>Re: Array manipulation question</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/154894#388647</link>
      <author>us</author>
      <description>Walter Roberson:&lt;br&gt;
&amp;lt;SNIP scolding a flawed solutions...&lt;br&gt;
&lt;br&gt;
&amp;gt; Adam wants the values in the original order (for whatever &lt;br&gt;
reason)...&lt;br&gt;
&lt;br&gt;
hmmmm, he/she didn't say so explicitly - the (unfortunate?) &lt;br&gt;
example could just have been a descending sort - naturally, &lt;br&gt;
i assume you are correct...&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
  </channel>
</rss>

