Thread Subject: How to sort cell array of cell arrays by inner value

Subject: How to sort cell array of cell arrays by inner value

From: "G.A.M.

Date: 10 Oct, 2007 02:37:31

Message: 1 of 7

I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The
inner 1 x 4 cell array has a double numeric value in the 4th cell. I
would like to sort the 500 cells by this numeric value. What is the
best way to do this? Thanks

Subject: How to sort cell array of cell arrays by inner value

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 10 Oct, 2007 05:30:42

Message: 2 of 7

In article <1191983851.468137.61110@r29g2000hsg.googlegroups.com>,
G.A.M. <x0Zero@gmail.com> wrote:
>I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The
>inner 1 x 4 cell array has a double numeric value in the 4th cell. I
>would like to sort the 500 cells by this numeric value. What is the
>best way to do this? Thanks

Use cellfun to extract just the value to be sorted on into an
array. Sort that array, using the two-output sort so that
you get the indices. Then index the original cell array at that
index list.

--
   "Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson

Subject: How to sort cell array of cell arrays by inner value

From: "G.A.M.

Date: 11 Oct, 2007 03:31:57

Message: 3 of 7

On Oct 10, 1:30 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <1191983851.468137.61...@r29g2000hsg.googlegroups.com>,
>
> G.A.M. <x0Z...@gmail.com> wrote:
> >I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The
> >inner 1 x 4 cell array has a double numeric value in the 4th cell. I
> >would like to sort the 500 cells by this numeric value. What is the
> >best way to do this? Thanks
>
> Use cellfun to extract just the value to be sorted on into an
> array. Sort that array, using the two-output sort so that
> you get the indices. Then index the original cell array at that
> index list.
>
> --
> "Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson

I appreciate the guidance. However, it looks like I need an example
because I can't figure out the cellfun part. Here's some sample data.
I want to sort AA by the numeric values in the 4th column of the inner
cells.

a = {'ay', 'd1', {'iala', 'lbb'}, 4}
b = {'xi', 'a2', {'adoa', 'bab'}, 7}
c = {'um', 'e3', {'caaa', 'cyb'}, 3}
d = {'iw', '4t', {'ljik', 'lwb'}, 9}
AA = {b, a, c, d}

This is how I'm indexing into the numeric values I want to use for
sorting:
AA{1}{4}
ans = 7

A simple example of how to apply the advice given above would be most
helpful. Thanks.


Subject: How to sort cell array of cell arrays by inner value

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Oct, 2007 03:37:30

Message: 4 of 7

In article <1192073517.687686.113540@19g2000hsx.googlegroups.com>,
G.A.M. <x0Zero@gmail.com> wrote:
>On Oct 10, 1:30 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
>wrote:
>> In article <1191983851.468137.61...@r29g2000hsg.googlegroups.com>,

>> G.A.M. <x0Z...@gmail.com> wrote:
>> >I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The
>> >inner 1 x 4 cell array has a double numeric value in the 4th cell. I
>> >would like to sort the 500 cells by this numeric value.

>Here's some sample data.
>I want to sort AA by the numeric values in the 4th column of the inner
>cells.

a = {'ay', 'd1', {'iala', 'lbb'}, 4}
b = {'xi', 'a2', {'adoa', 'bab'}, 7}
c = {'um', 'e3', {'caaa', 'cyb'}, 3}
d = {'iw', '4t', {'ljik', 'lwb'}, 9}
AA = {b, a, c, d}

>This is how I'm indexing into the numeric values I want to use for
>sorting:
>AA{1}{4}
>ans = 7

[vals,order] = cellfun(@(v) v{4},AA);
sortedAA = AA(order);
--
   "History is a pile of debris" -- Laurie Anderson

Subject: How to sort cell array of cell arrays by inner value

From: "G.A.M.

Date: 11 Oct, 2007 05:06:04

Message: 5 of 7

On Oct 10, 11:37 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <1192073517.687686.113...@19g2000hsx.googlegroups.com>,
>
> G.A.M. <x0Z...@gmail.com> wrote:
> >On Oct 10, 1:30 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
> >wrote:
> >> In article <1191983851.468137.61...@r29g2000hsg.googlegroups.com>,
> >> G.A.M. <x0Z...@gmail.com> wrote:
> >> >I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The
> >> >inner 1 x 4 cell array has a double numeric value in the 4th cell. I
> >> >would like to sort the 500 cells by this numeric value.
> >Here's some sample data.
> >I want to sort AA by the numeric values in the 4th column of the inner
> >cells.
>
> a = {'ay', 'd1', {'iala', 'lbb'}, 4}
> b = {'xi', 'a2', {'adoa', 'bab'}, 7}
> c = {'um', 'e3', {'caaa', 'cyb'}, 3}
> d = {'iw', '4t', {'ljik', 'lwb'}, 9}
> AA = {b, a, c, d}
>
> >This is how I'm indexing into the numeric values I want to use for
> >sorting:
> >AA{1}{4}
> >ans = 7
>
> [vals,order] = cellfun(@(v) v{4},AA);
> sortedAA = AA(order);
> --
> "History is a pile of debris" -- Laurie Anderson

Thanks!

Subject: How to sort cell array of cell arrays by inner value

From: "G.A.M.

Date: 11 Oct, 2007 05:29:44

Message: 6 of 7

On Oct 10, 11:37 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <1192073517.687686.113...@19g2000hsx.googlegroups.com>,
>
> G.A.M. <x0Z...@gmail.com> wrote:
> >On Oct 10, 1:30 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
> >wrote:
> >> In article <1191983851.468137.61...@r29g2000hsg.googlegroups.com>,
> >> G.A.M. <x0Z...@gmail.com> wrote:
> >> >I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The
> >> >inner 1 x 4 cell array has a double numeric value in the 4th cell. I
> >> >would like to sort the 500 cells by this numeric value.
> >Here's some sample data.
> >I want to sort AA by the numeric values in the 4th column of the inner
> >cells.
>
> a = {'ay', 'd1', {'iala', 'lbb'}, 4}
> b = {'xi', 'a2', {'adoa', 'bab'}, 7}
> c = {'um', 'e3', {'caaa', 'cyb'}, 3}
> d = {'iw', '4t', {'ljik', 'lwb'}, 9}
> AA = {b, a, c, d}
>
> >This is how I'm indexing into the numeric values I want to use for
> >sorting:
> >AA{1}{4}
> >ans = 7
>
> [vals,order] = cellfun(@(v) v{4},AA);
> sortedAA = AA(order);
> --
> "History is a pile of debris" -- Laurie Anderson

When I run this, I get this error:
Insufficient number of outputs from right hand side of equal sign to
satisfy assignment.

Seems like a separate call to sort() may be required. I was hoping for
a compact version like what was given here, but I can't seem to get it
to work.

Subject: How to sort cell array of cell arrays by inner value

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Oct, 2007 13:10:32

Message: 7 of 7

In article <fek5pq$qi8$1@canopus.cc.umanitoba.ca>,
Walter Roberson <roberson@ibd.nrc-cnrc.gc.ca> wrote:

>[vals,order] = cellfun(@(v) v{4},AA);
>sortedAA = AA(order);

Sorry, I did test, but I mistyped when I posted.

[vals,order] = sort(cellfun(@(v) v{4},AA));
sortedAA = AA(order);
--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com