Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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 9

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 9

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 9

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 9

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 9

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 9

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 9

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

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

From: Rajesh

Date: 14 Dec, 2010 09:02:06

Message: 8 of 9

thanks walter,
i am facing a similar problem and seek a way out of it.
I have a 3 dmensional cell array containing some two dimensional cells arrays containing numeric arrays. as follows
A=main cell array
B=array within A

say,
B=A{20,30,1} (B is a 4x4 cell array)
B=[-0.22] [-0.11]; [0.19] [-0.26];[0.17][-0.25];[-0.08][-0.16];

here, I want to sort elements of B wrt first column. When i apply above method,

[val ord]=sort(cellfun(@f f(1), B));
srted=B(ord);

i get,
srted=[-0.22] [-0.26];[-0.08] [-0.25]; [0.17] [-0.16]; [0.19] [-0.11];

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.

any way out?

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

From: Rajesh

Date: 14 Dec, 2010 10:01:07

Message: 9 of 9

Sorry for the typo above
B is a 4 x 2 cell array as follows
B={[-0.22] [-0.11]; [0.19] [-0.26];[0.17][-0.25];[-0.08][-0.16]}; (same numbers as above)

Tags for this Thread

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.

Contact us