Azzera filtri
Azzera filtri

Sorting the variables in cells

1 visualizzazione (ultimi 30 giorni)
Pat
Pat il 4 Set 2012
in r=
{6x4 cell}
{6x4 cell}
{5x4 cell}
{4x4 cell}
{3x4 cell}
{2x4 cell}
r{1,1}
'Genes' 'T0&T2' [] 'perc'
'YBL113C' 'u' [] 60
'YBR138C' 'du' 3 100
'YBR285W' 'du' 3 80
'YCL056C' 'dd' 3 80
'YDR042C' 'dd' 3 60
'YDR286C' 'uu' 3 100
'YDR476C' 'uu' 3 80
'YER185W' 'du' 3 80
'YGR015C' 'dd' 3 100
'YGR035C' 'dd' 3 60
i want to sort rows according to second column
In 2nd column i have 'du','ud','uu','dd' mingled
i want to sort them as
'Genes' 'T0&T2' [] 'perc'
'YBL113C' 'u' [] 60
'YBR138C' 'du' 3 100
'YBR285W' 'du' 3 80
'YER185W' 'du' 3 80
'YCL056C' 'dd' 3 80
'YDR042C' 'dd' 3 60
'YDR286C' 'uu' 3 100
'YDR476C' 'uu' 3 80
please help
  2 Commenti
Jan
Jan il 4 Set 2012
What have you tried so far?
Pat
Pat il 5 Set 2012
i tried
for m = 1:numel(r)
t =cell2mat(cellfun(@x sort(r,2,'uni',false)
final{m} = t;
end

Accedi per commentare.

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 5 Set 2012
try this is code
s = {'du';'dd';'uu';'ud'};
r2 = r;
for jj = 1:numel(r2)
q = r2{jj}(3:end,2);
[i1,i1] = ismember(q,s);
[i2,i2] = sort(i1);
r2{jj} = r2{jj}([1:2,i2(:)'+2],:);
end

Più risposte (2)

Kevin Claytor
Kevin Claytor il 4 Set 2012
I don't know of a built-in function that sorts cell arrays. I did a similar task awhile ago and had to pull out the data into a vector and sort that - you can get the sort order back, and use that to resort the cell array.
There's also this entry in the FEX that may be of interest (haven't tried it though); http://www.mathworks.com/matlabcentral/fileexchange/13770-sorting-a-cell-array

Arthur
Arthur il 5 Set 2012
sortrows can sort cell arrays. This should work:
out = cellfun(@(x) sortrows(x,2),r,'uniformOutput',false)
the only problem you have is that you don't want to sort them alphabetically. I guess the easiest is to first the u, ud, uu etc with something else (A,B,C,...), sort the cells, and then change back to the original strings.

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by