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:
sorting

Subject: sorting

From: Jessica

Date: 5 Jul, 2008 14:31:02

Message: 1 of 7

I was wondering whether anyone has suggestions about
sorting cell arrays. For example, I have:

T={'9','M_21',10,20;'5', 'M_19',13,26};

I would like to be able to sort the array such that the
order of the values in each row remain the same but they
are sorted by column 2, such that:

T={'5', 'M_19',13,26; '9','M_21',10,20};

I can't use the rowsort command because the values in the
cells are not of the same type. Does anyone have any
suggestions?

(I can do this manually by saving "T" as an Excel file and
then Sort it there, but it would be nice to do this all
within Matlab.)

Thanks!

Subject: sorting

From: Per Sundqvist

Date: 5 Jul, 2008 16:58:01

Message: 2 of 7

"Jessica " <jyorzinski@ucdavis.edu> wrote in message
<g4o0j5$e3m$1@fred.mathworks.com>...
> I was wondering whether anyone has suggestions about
> sorting cell arrays. For example, I have:
>
> T={'9','M_21',10,20;'5', 'M_19',13,26};
>
> I would like to be able to sort the array such that the
> order of the values in each row remain the same but they
> are sorted by column 2, such that:
>
> T={'5', 'M_19',13,26; '9','M_21',10,20};
>
> I can't use the rowsort command because the values in the
> cells are not of the same type. Does anyone have any
> suggestions?
>
> (I can do this manually by saving "T" as an Excel file and
> then Sort it there, but it would be nice to do this all
> within Matlab.)
>

Maby you could work it out from here. It looks like sort fix
the M_19 sorting, but then I don't know why it does not work:

T =

    '9' 'M_21' [10] [20]
    '5' 'M_19' [13] [26]

>> [dummy,ix]=sort(T(:,2))

dummy =

    'M_19'
    'M_21'


ix =

     2
     1

>> T(:,ix)

ans =

    'M_21' '9'
    'M_19' '5'
/Per

Subject: sorting

From: Jos

Date: 7 Jul, 2008 06:35:03

Message: 3 of 7

"Per Sundqvist" <sunkan@fy.chalmers.se> wrote in message
<g4o96p$bng$1@fred.mathworks.com>...
> "Jessica " <jyorzinski@ucdavis.edu> wrote in message
> <g4o0j5$e3m$1@fred.mathworks.com>...
> > I was wondering whether anyone has suggestions about
> > sorting cell arrays. For example, I have:
> >
> > T={'9','M_21',10,20;'5', 'M_19',13,26};
> >
> > I would like to be able to sort the array such that the
> > order of the values in each row remain the same but they
> > are sorted by column 2, such that:
> >
> > T={'5', 'M_19',13,26; '9','M_21',10,20};
> >
> > I can't use the rowsort command because the values in the
> > cells are not of the same type. Does anyone have any
> > suggestions?
> >
> > (I can do this manually by saving "T" as an Excel file and
> > then Sort it there, but it would be nice to do this all
> > within Matlab.)
> >
>
> Maby you could work it out from here. It looks like sort fix
> the M_19 sorting, but then I don't know why it does not work:
>
> T =
>
> '9' 'M_21' [10] [20]
> '5' 'M_19' [13] [26]
>
> >> [dummy,ix]=sort(T(:,2))
>
> dummy =
>
> 'M_19'
> 'M_21'
>
>
> ix =
>
> 2
> 1
>
> >> T(:,ix)
>
> ans =
>
> 'M_21' '9'
> 'M_19' '5'
> /Per
>
>


A small correction to Per's excellent hint:

[dummy,ix]=sort(T(:,2))
sortedT = T(ix,:) % <-- and not T(:,ix) !!

hth
Jos

Subject: sorting

From: Jos

Date: 7 Jul, 2008 06:46:01

Message: 4 of 7

"Jessica " <jyorzinski@ucdavis.edu> wrote in message
<g4o0j5$e3m$1@fred.mathworks.com>...
 
> T={'5', 'M_19',13,26; '9','M_21',10,20};
>
> I can't use the rowsort command because the values in the
> cells are not of the same type. Does anyone have any
> suggestions?


Hmm, on second thoughts, SORTROWS should work here!

>> T={'9','M_21',10,20;'5', 'M_19',13,26}
T =
    '9' 'M_21' [10] [20]
    '5' 'M_19' [13] [26]
>> sortrows(T,2)
ans =

    '5' 'M_19' [13] [26]
    '9' 'M_21' [10] [20]


Jos

Subject: sorting

From: us

Date: 7 Jul, 2008 08:31:02

Message: 5 of 7

"Jessica ":
<SNIP a matter of english...

> I can't use the rowsort command because the values in the
> cells are not of the same type...

but - you can use SORTROWS...

     c={
          '9' 'M_21' [10] [20]
          '5' 'M_19' [13] [26]
     };
     cs=sortrows(c,1)
%{
     cs =
     '5' 'M_19' [13] [26]
     '9' 'M_21' [10] [20]
%}

us

Subject: sorting

From: Jessica

Date: 7 Jul, 2008 13:01:03

Message: 6 of 7

"us " <us@neurol.unizh.ch> wrote in message <g4sk86$8d9
$1@fred.mathworks.com>...
> "Jessica ":
> <SNIP a matter of english...
>
> > I can't use the rowsort command because the values in
the
> > cells are not of the same type...
>
> but - you can use SORTROWS...
>
> c={
> '9' 'M_21' [10] [20]
> '5' 'M_19' [13] [26]
> };
> cs=sortrows(c,1)
> %{
> cs =
> '5' 'M_19' [13] [26]
> '9' 'M_21' [10] [20]
> %}
>
> us

When I use SORTROWS on my data I get:

??? Error using ==> char
Cell elements must be character arrays.

Error in ==> sortrows>sort_cell_back_to_front at 129
    tmp = char(x(ndx,k));

Error in ==> sortrows at 90
        ndx = sort_cell_back_to_front(x_sub, col);

So it doesn't seem to work for me...

Subject: sorting

From: ravi.chilumula@gmail.com

Date: 7 Jul, 2008 14:49:30

Message: 7 of 7

sorts the two rows based on the order in column 2...it works fine R2008a...

T={'9','M_21',10,20;'5', 'M_19',13,26};
sortrows(T,2)

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