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
采纳的回答
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
0 个评论
更多回答(2 个)
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
0 个评论
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 Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!