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
채택된 답변
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!