Sorting the variables in cells

2 次查看(过去 30 天)
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 个评论
Jan
Jan 2012-9-4
What have you tried so far?
Pat
Pat 2012-9-5
i tried
for m = 1:numel(r)
t =cell2mat(cellfun(@x sort(r,2,'uni',false)
final{m} = t;
end

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-9-5
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

更多回答(2 个)

Kevin Claytor
Kevin Claytor 2012-9-4
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 2012-9-5
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.

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by