Sorting the variables in cells
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Andrei Bobrov
on 5 Sep 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
0 Comments
More Answers (2)
Kevin Claytor
on 4 Sep 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
0 Comments
Arthur
on 5 Sep 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.
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!