Thread Subject: sorting cell

Subject: sorting cell

From: parham

Date: 28 Jun, 2009 06:01:11

Message: 1 of 10

hi
I have a problem.I have a cell wich is in below, and as you see its size is 1*3
 
c={{83 85 84 86 86 86 94 83 83 }{19 20 19 12 16 15 13 18 15} {'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}}
fierst column is entrance year and second is average.How can I sort this cell according to entrance year and if the entrance year are equal then it sorts according to average.for example
c={{83 83 83 84 85 86 86 86 94}{15 18 19 19 20 12 15 16 15}{{'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}}
thanks in advance

Subject: sorting cell

From: us

Date: 28 Jun, 2009 07:19:02

Message: 2 of 10

parham <parham_iut@yahoo.com> wrote in message <9053934.40651.1246168902401.JavaMail.jakarta@nitrogen.mathforum.org>...
> hi
> I have a problem.I have a cell wich is in below, and as you see its size is 1*3
>
> c={{83 85 84 86 86 86 94 83 83 }{19 20 19 12 16 15 13 18 15} {'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}}
> fierst column is entrance year and second is average.How can I sort this cell according to entrance year and if the entrance year are equal then it sorts according to average.for example
> c={{83 83 83 84 85 86 86 86 94}{15 18 19 19 20 12 15 16 15}{{'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}}
> thanks in advance

as was replied in one of your previous OPs: look at SORTROWS...
did you even bother...

     help sortrows;
% -and- to prepare your particular data set C, peruse
     help cat;

us

Subject: sorting cell

From: Bruno Luong

Date: 28 Jun, 2009 07:23:00

Message: 3 of 10

parham <parham_iut@yahoo.com> wrote in message <9053934.40651.1246168902401.JavaMail.jakarta@nitrogen.mathforum.org>...
> hi
> I have a problem.I have a cell wich is in below, and as you see its size is 1*3
>
> c={{83 85 84 86 86 86 94 83 83 }{19 20 19 12 16 15 13 18 15} {'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}}
> fierst column is entrance year and second is average.How can I sort this cell according to entrance year and if the entrance year are equal then it sorts according to average.for example
> c={{83 83 83 84 85 86 86 86 94}{15 18 19 19 20 12 15 16 15}{{'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}}
> thanks in advance

Use SORT or SORTROWS, both require a formatting of your data first.

Bruno

Subject: sorting cell

From: parham

Date: 28 Jun, 2009 08:41:16

Message: 4 of 10

yeees but sortrows is for chracter,when I use it matlab error me

Subject: sorting cell

From: us

Date: 28 Jun, 2009 09:33:01

Message: 5 of 10

parham <parham_iut@yahoo.com> wrote in message <30304573.41024.1246178507734.JavaMail.jakarta@nitrogen.mathforum.org>...
> yeees but sortrows is for chracter,when I use it matlab error me

NO - SORTROWS is able to handle cells of (mixed) numeric/logical/character content...
see the help/doc...

us

Subject: sorting cell

From: Bruno Luong

Date: 28 Jun, 2009 10:04:02

Message: 6 of 10

parham <parham_iut@yahoo.com> wrote in message <30304573.41024.1246178507734.JavaMail.jakarta@nitrogen.mathforum.org>...
> yeees but sortrows is for chracter,when I use it matlab error me

You should do formatting of your data. That why the doc is for. Understand format data and be able to do conversion between them is the key in programming.

Bruno

Subject: sorting cell

From: Oleg Komarov

Date: 28 Jun, 2009 12:21:01

Message: 7 of 10

I feel kinda sorry and i think that one chance may be given from time to time.
To: Parham
Read the code and the help files of the functions in order to understand how they work. Don't ask just to solve problems for u. This is a long run-advice.

% Your example:
c={{83 83 83 84 85 86 86 86 94} {15 18 19 19 20 12 15 16 15} {'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}};

% Counts the number of types of information
Col = size(c,2);

% CONDITION if u have all "Col" datatypes for every entry
if ~all(diff(cellfun(@numel, c)))
    % Flattening (this is my/Bruno's :) function) http://www.mathworks.com/matlabcentral/fileexchange/24544
    c = flatcell(c);
    % Reshaping
    c = reshape(c, [], Col);
    % Sorting by the first and then by the second
    c = sortrows(c, [1 2]);
else
    display('Missing data')
end


By the way, there is also this tool that may come in handy http://www.mathworks.com/matlabcentral/fileexchange/14225

Subject: sorting cell

From: Oleg Komarov

Date: 28 Jun, 2009 12:22:02

Message: 8 of 10

I feel kinda sorry and i think that one chance may be given from time to time.
To: Parham
Read the code and the help files of the functions in order to understand how they work. Don't ask just to solve problems for u. This is a long run-advice.

% Your example:
c={{83 83 83 84 85 86 86 86 94} {15 18 19 19 20 12 15 16 15} {'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}};

% Counts the number of types of information
Col = size(c,2);

% CONDITION if u have all "Col" datatypes for every entry
if ~all(diff(cellfun(@numel, c)))
    % Flattening (this is my/Bruno's :) function) http://www.mathworks.com/matlabcentral/fileexchange/24544
    c = flatcell(c);
    % Reshaping
    c = reshape(c, [], Col);
    % Sorting by the first and then by the second
    c = sortrows(c, [1 2]);
else
    display('Missing data')
end


By the way, there is also this tool that may come in handy http://www.mathworks.com/matlabcentral/fileexchange/14225

Subject: sorting cell

From: us

Date: 28 Jun, 2009 12:39:01

Message: 9 of 10

"Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <h27n99$fbi$1@fred.mathworks.com>...
> I feel kinda sorry and i think that one chance may be given from time to time.
> To: Parham
> Read the code and the help files of the functions in order to understand how they work. Don't ask just to solve problems for u. This is a long run-advice.
>
> % Your example:
> c={{83 83 83 84 85 86 86 86 94} {15 18 19 19 20 12 15 16 15} {'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}};
>
> % Counts the number of types of information
> Col = size(c,2);
>
> % CONDITION if u have all "Col" datatypes for every entry
> if ~all(diff(cellfun(@numel, c)))
> % Flattening (this is my/Bruno's :) function) http://www.mathworks.com/matlabcentral/fileexchange/24544
> c = flatcell(c);
> % Reshaping
> c = reshape(c, [], Col);
> % Sorting by the first and then by the second
> c = sortrows(c, [1 2]);
> else
> display('Missing data')
> end
>
>
> By the way, there is also this tool that may come in handy http://www.mathworks.com/matlabcentral/fileexchange/14225

why...
the OP was asked to look at SORTROWS and CAT, which do the job just fine...

one of the solutions

% the (ori) data
     c={ % <- 1 x 3 cell array of cells
          {83 85 84 86 86 86 94 83 83},...
          {19 20 19 12 16 15 13 18 15},...
          {'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}
     };
% the engine
     cc=cat(1,c{:}).';
     r=sortrows(cc,[1,2]);
% the result
     disp(r);
%{
          [83] [15] 'x'
          [83] [18] 'r'
          [83] [19] 'b'
          [84] [19] 'a'
          [85] [20] 'c'
          [86] [12] 't'
          [86] [15] 'r'
          [86] [16] 'f'
          [94] [13] 'd'
%}

us

Subject: sorting cell

From: Oleg Komarov

Date: 28 Jun, 2009 13:14:01

Message: 10 of 10

"us " <us@neurol.unizh.ch> wrote in message <h27o95$hnd$1@fred.mathworks.com>...
> "Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <h27n99$fbi$1@fred.mathworks.com>...
> > I feel kinda sorry and i think that one chance may be given from time to time.
> > To: Parham
> > Read the code and the help files of the functions in order to understand how they work. Don't ask just to solve problems for u. This is a long run-advice.
> >
> > % Your example:
> > c={{83 83 83 84 85 86 86 86 94} {15 18 19 19 20 12 15 16 15} {'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}};
> >
> > % Counts the number of types of information
> > Col = size(c,2);
> >
> > % CONDITION if u have all "Col" datatypes for every entry
> > if ~all(diff(cellfun(@numel, c)))
> > % Flattening (this is my/Bruno's :) function) http://www.mathworks.com/matlabcentral/fileexchange/24544
> > c = flatcell(c);
> > % Reshaping
> > c = reshape(c, [], Col);
> > % Sorting by the first and then by the second
> > c = sortrows(c, [1 2]);
> > else
> > display('Missing data')
> > end
> >
> >
> > By the way, there is also this tool that may come in handy http://www.mathworks.com/matlabcentral/fileexchange/14225
>
> why...
> the OP was asked to look at SORTROWS and CAT, which do the job just fine...
>
> one of the solutions
>
> % the (ori) data
> c={ % <- 1 x 3 cell array of cells
> {83 85 84 86 86 86 94 83 83},...
> {19 20 19 12 16 15 13 18 15},...
> {'b' 'c' 'a' 't' 'f' 'r' 'd' 'r' 'x'}
> };
> % the engine
> cc=cat(1,c{:}).';
> r=sortrows(cc,[1,2]);
> % the result
> disp(r);
> %{
> [83] [15] 'x'
> [83] [18] 'r'
> [83] [19] 'b'
> [84] [19] 'a'
> [85] [20] 'c'
> [86] [12] 't'
> [86] [15] 'r'
> [86] [16] 'f'
> [94] [13] 'd'
> %}
>
> us

Deeply right!

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
sortrows us 28 Jun, 2009 08:44:08
code us 28 Jun, 2009 08:44:08
cat us 28 Jun, 2009 08:44:08
reference us 28 Jun, 2009 03:24:03
rssFeed for this Thread

Contact us at files@mathworks.com