<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426</link>
    <title>MATLAB Central Newsreader - How to sort cell array of cell arrays by inner value</title>
    <description>Feed for thread: How to sort cell array of cell arrays by inner value</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>Wed, 10 Oct 2007 02:37:31 -0400</pubDate>
      <title>How to sort cell array of cell arrays by inner value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426#395882</link>
      <author> &quot;G.A.M.</author>
      <description>I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The&lt;br&gt;
inner 1 x 4 cell array has a double numeric value in the 4th cell. I&lt;br&gt;
would like to sort the 500 cells by this numeric value. What is the&lt;br&gt;
best way to do this? Thanks</description>
    </item>
    <item>
      <pubDate>Wed, 10 Oct 2007 05:30:42 -0400</pubDate>
      <title>Re: How to sort cell array of cell arrays by inner value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426#395890</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;1191983851.468137.61110@r29g2000hsg.googlegroups.com&amp;gt;,&lt;br&gt;
G.A.M. &amp;lt;x0Zero@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The&lt;br&gt;
&amp;gt;inner 1 x 4 cell array has a double numeric value in the 4th cell. I&lt;br&gt;
&amp;gt;would like to sort the 500 cells by this numeric value. What is the&lt;br&gt;
&amp;gt;best way to do this? Thanks&lt;br&gt;
&lt;br&gt;
Use cellfun to extract just the value to be sorted on into an&lt;br&gt;
array. Sort that array, using the two-output sort so that&lt;br&gt;
you get the indices. Then index the original cell array at that&lt;br&gt;
index list.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;Okay, buzzwords only. Two syllables, tops.&quot;  -- Laurie Anderson</description>
    </item>
    <item>
      <pubDate>Thu, 11 Oct 2007 03:31:57 -0400</pubDate>
      <title>Re: How to sort cell array of cell arrays by inner value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426#396122</link>
      <author> &quot;G.A.M.</author>
      <description>On Oct 10, 1:30 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)&lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; In article &amp;lt;1191983851.468137.61...@r29g2000hsg.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; G.A.M. &amp;lt;x0Z...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt;I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The&lt;br&gt;
&amp;gt; &amp;gt;inner 1 x 4 cell array has a double numeric value in the 4th cell. I&lt;br&gt;
&amp;gt; &amp;gt;would like to sort the 500 cells by this numeric value. What is the&lt;br&gt;
&amp;gt; &amp;gt;best way to do this? Thanks&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Use cellfun to extract just the value to be sorted on into an&lt;br&gt;
&amp;gt; array. Sort that array, using the two-output sort so that&lt;br&gt;
&amp;gt; you get the indices. Then index the original cell array at that&lt;br&gt;
&amp;gt; index list.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; --&lt;br&gt;
&amp;gt;    &quot;Okay, buzzwords only. Two syllables, tops.&quot;  -- Laurie Anderson&lt;br&gt;
&lt;br&gt;
I appreciate the guidance. However, it looks like I need an example&lt;br&gt;
because I can't figure out the cellfun part. Here's some sample data.&lt;br&gt;
I want to sort AA by the numeric values in the 4th column of the inner&lt;br&gt;
cells.&lt;br&gt;
&lt;br&gt;
a = {'ay', 'd1', {'iala', 'lbb'}, 4}&lt;br&gt;
b = {'xi', 'a2', {'adoa', 'bab'}, 7}&lt;br&gt;
c = {'um', 'e3', {'caaa', 'cyb'}, 3}&lt;br&gt;
d = {'iw', '4t', {'ljik', 'lwb'}, 9}&lt;br&gt;
AA = {b, a, c, d}&lt;br&gt;
&lt;br&gt;
This is how I'm indexing into the numeric values I want to use for&lt;br&gt;
sorting:&lt;br&gt;
AA{1}{4}&lt;br&gt;
ans = 7&lt;br&gt;
&lt;br&gt;
A simple example of how to apply the advice given above would be most&lt;br&gt;
helpful. Thanks.</description>
    </item>
    <item>
      <pubDate>Thu, 11 Oct 2007 03:37:30 -0400</pubDate>
      <title>Re: How to sort cell array of cell arrays by inner value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426#396125</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;1192073517.687686.113540@19g2000hsx.googlegroups.com&amp;gt;,&lt;br&gt;
G.A.M. &amp;lt;x0Zero@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;On Oct 10, 1:30 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)&lt;br&gt;
&amp;gt;wrote:&lt;br&gt;
&amp;gt;&amp;gt; In article &amp;lt;1191983851.468137.61...@r29g2000hsg.googlegroups.com&amp;gt;,&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; G.A.M. &amp;lt;x0Z...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;&amp;gt; &amp;gt;I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The&lt;br&gt;
&amp;gt;&amp;gt; &amp;gt;inner 1 x 4 cell array has a double numeric value in the 4th cell. I&lt;br&gt;
&amp;gt;&amp;gt; &amp;gt;would like to sort the 500 cells by this numeric value.&lt;br&gt;
&lt;br&gt;
&amp;gt;Here's some sample data.&lt;br&gt;
&amp;gt;I want to sort AA by the numeric values in the 4th column of the inner&lt;br&gt;
&amp;gt;cells.&lt;br&gt;
&lt;br&gt;
a = {'ay', 'd1', {'iala', 'lbb'}, 4}&lt;br&gt;
b = {'xi', 'a2', {'adoa', 'bab'}, 7}&lt;br&gt;
c = {'um', 'e3', {'caaa', 'cyb'}, 3}&lt;br&gt;
d = {'iw', '4t', {'ljik', 'lwb'}, 9}&lt;br&gt;
AA = {b, a, c, d}&lt;br&gt;
&lt;br&gt;
&amp;gt;This is how I'm indexing into the numeric values I want to use for&lt;br&gt;
&amp;gt;sorting:&lt;br&gt;
&amp;gt;AA{1}{4}&lt;br&gt;
&amp;gt;ans = 7&lt;br&gt;
&lt;br&gt;
[vals,order] = cellfun(@(v) v{4},AA);&lt;br&gt;
sortedAA = AA(order);&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;History is a pile of debris&quot;                     -- Laurie Anderson</description>
    </item>
    <item>
      <pubDate>Thu, 11 Oct 2007 05:06:04 -0400</pubDate>
      <title>Re: How to sort cell array of cell arrays by inner value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426#396132</link>
      <author> &quot;G.A.M.</author>
      <description>On Oct 10, 11:37 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)&lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; In article &amp;lt;1192073517.687686.113...@19g2000hsx.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; G.A.M. &amp;lt;x0Z...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt;On Oct 10, 1:30 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)&lt;br&gt;
&amp;gt; &amp;gt;wrote:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; In article &amp;lt;1191983851.468137.61...@r29g2000hsg.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; G.A.M. &amp;lt;x0Z...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; &amp;gt;I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; &amp;gt;inner 1 x 4 cell array has a double numeric value in the 4th cell. I&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; &amp;gt;would like to sort the 500 cells by this numeric value.&lt;br&gt;
&amp;gt; &amp;gt;Here's some sample data.&lt;br&gt;
&amp;gt; &amp;gt;I want to sort AA by the numeric values in the 4th column of the inner&lt;br&gt;
&amp;gt; &amp;gt;cells.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; a = {'ay', 'd1', {'iala', 'lbb'}, 4}&lt;br&gt;
&amp;gt; b = {'xi', 'a2', {'adoa', 'bab'}, 7}&lt;br&gt;
&amp;gt; c = {'um', 'e3', {'caaa', 'cyb'}, 3}&lt;br&gt;
&amp;gt; d = {'iw', '4t', {'ljik', 'lwb'}, 9}&lt;br&gt;
&amp;gt; AA = {b, a, c, d}&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;This is how I'm indexing into the numeric values I want to use for&lt;br&gt;
&amp;gt; &amp;gt;sorting:&lt;br&gt;
&amp;gt; &amp;gt;AA{1}{4}&lt;br&gt;
&amp;gt; &amp;gt;ans = 7&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [vals,order] = cellfun(@(v) v{4},AA);&lt;br&gt;
&amp;gt; sortedAA = AA(order);&lt;br&gt;
&amp;gt; --&lt;br&gt;
&amp;gt;    &quot;History is a pile of debris&quot;                     -- Laurie Anderson&lt;br&gt;
&lt;br&gt;
Thanks!</description>
    </item>
    <item>
      <pubDate>Thu, 11 Oct 2007 05:29:44 -0400</pubDate>
      <title>Re: How to sort cell array of cell arrays by inner value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426#396133</link>
      <author> &quot;G.A.M.</author>
      <description>On Oct 10, 11:37 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)&lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; In article &amp;lt;1192073517.687686.113...@19g2000hsx.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; G.A.M. &amp;lt;x0Z...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt;On Oct 10, 1:30 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)&lt;br&gt;
&amp;gt; &amp;gt;wrote:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; In article &amp;lt;1191983851.468137.61...@r29g2000hsg.googlegroups.com&amp;gt;,&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; G.A.M. &amp;lt;x0Z...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; &amp;gt;I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; &amp;gt;inner 1 x 4 cell array has a double numeric value in the 4th cell. I&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; &amp;gt;would like to sort the 500 cells by this numeric value.&lt;br&gt;
&amp;gt; &amp;gt;Here's some sample data.&lt;br&gt;
&amp;gt; &amp;gt;I want to sort AA by the numeric values in the 4th column of the inner&lt;br&gt;
&amp;gt; &amp;gt;cells.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; a = {'ay', 'd1', {'iala', 'lbb'}, 4}&lt;br&gt;
&amp;gt; b = {'xi', 'a2', {'adoa', 'bab'}, 7}&lt;br&gt;
&amp;gt; c = {'um', 'e3', {'caaa', 'cyb'}, 3}&lt;br&gt;
&amp;gt; d = {'iw', '4t', {'ljik', 'lwb'}, 9}&lt;br&gt;
&amp;gt; AA = {b, a, c, d}&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;This is how I'm indexing into the numeric values I want to use for&lt;br&gt;
&amp;gt; &amp;gt;sorting:&lt;br&gt;
&amp;gt; &amp;gt;AA{1}{4}&lt;br&gt;
&amp;gt; &amp;gt;ans = 7&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; [vals,order] = cellfun(@(v) v{4},AA);&lt;br&gt;
&amp;gt; sortedAA = AA(order);&lt;br&gt;
&amp;gt; --&lt;br&gt;
&amp;gt;    &quot;History is a pile of debris&quot;                     -- Laurie Anderson&lt;br&gt;
&lt;br&gt;
When I run this, I get this error:&lt;br&gt;
Insufficient number of outputs from right hand side of equal sign to&lt;br&gt;
satisfy assignment.&lt;br&gt;
&lt;br&gt;
Seems like a separate call to sort() may be required. I was hoping for&lt;br&gt;
a compact version like what was given here, but I can't seem to get it&lt;br&gt;
to work.</description>
    </item>
    <item>
      <pubDate>Thu, 11 Oct 2007 13:10:32 -0400</pubDate>
      <title>Re: How to sort cell array of cell arrays by inner value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426#396193</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fek5pq$qi8$1@canopus.cc.umanitoba.ca&amp;gt;,&lt;br&gt;
Walter Roberson &amp;lt;roberson@ibd.nrc-cnrc.gc.ca&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;[vals,order] = cellfun(@(v) v{4},AA);&lt;br&gt;
&amp;gt;sortedAA = AA(order);&lt;br&gt;
&lt;br&gt;
Sorry, I did test, but I mistyped when I posted.&lt;br&gt;
&lt;br&gt;
[vals,order] = sort(cellfun(@(v) v{4},AA));&lt;br&gt;
sortedAA = AA(order);&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;Beware of bugs in the above code; I have only proved it correct,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;not tried it.&quot;                                    -- Donald Knuth</description>
    </item>
    <item>
      <pubDate>Tue, 14 Dec 2010 09:02:06 -0500</pubDate>
      <title>Re: How to sort cell array of cell arrays by inner value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426#804582</link>
      <author>Rajesh </author>
      <description>thanks walter, &lt;br&gt;
i am facing a similar problem and seek a way out of it.&lt;br&gt;
I have a 3 dmensional cell array containing some two dimensional cells arrays containing numeric arrays. as follows&lt;br&gt;
A=main cell array&lt;br&gt;
B=array within A&lt;br&gt;
&lt;br&gt;
say,&lt;br&gt;
B=A{20,30,1}   (B is a 4x4 cell array)&lt;br&gt;
B=[-0.22] [-0.11]; [0.19] [-0.26];[0.17][-0.25];[-0.08][-0.16];&lt;br&gt;
&lt;br&gt;
here, I want to sort elements of B wrt first column. When i apply above method,&lt;br&gt;
&lt;br&gt;
[val ord]=sort(cellfun(@f f(1), B));&lt;br&gt;
srted=B(ord); &lt;br&gt;
&lt;br&gt;
i get,&lt;br&gt;
srted=[-0.22] [-0.26];[-0.08] [-0.25]; [0.17] [-0.16]; [0.19] [-0.11];&lt;br&gt;
&lt;br&gt;
I find that both the columns are sorted independently and therefore the correspondence is lost. How to sort without losing this correspondence. I think because the numbers in B are in independent arrays and not in a single 4x4 numeric array that i am getting such output. &lt;br&gt;
&lt;br&gt;
any way out?</description>
    </item>
    <item>
      <pubDate>Tue, 14 Dec 2010 10:01:07 -0500</pubDate>
      <title>Re: How to sort cell array of cell arrays by inner value</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/157426#804594</link>
      <author>Rajesh </author>
      <description>Sorry for the typo above&lt;br&gt;
B is a 4 x 2 cell array as follows&lt;br&gt;
B={[-0.22] [-0.11]; [0.19] [-0.26];[0.17][-0.25];[-0.08][-0.16]}; (same numbers as above)</description>
    </item>
  </channel>
</rss>

