Cell_in = {'a1','b1','b3','c3','c1','d3'}; % something like this
Cell_out = {'a1','b1','c1';
'b3','c3','d3';...
}
等等,怎么办呢?
答案 0 :(得分:2)
案例1:一致的尺寸
%%// Input (different from question for a better demo)
Cell_in = {'airplane1','bat1','ball3','cat3','coal1','doggie3'};
ids = cellfun(@(x) x(end), Cell_in,'uni',0)
[~,ind] = sort(ids)
Cell_out = reshape(Cell_in(ind),[],numel(unique(ids)))' %%// Output
<强>输出强>
Cell_out =
'airplane1' 'bat1' 'coal1'
'ball3' 'cat3' 'doggie3'
案例2:不一致的尺寸
Cell_in = {'airplane1','bat1','ball3','cat3','coal1','doggie3','cat2','ball2'};
ids = cellfun(@(x) x(end), Cell_in,'uni',0)
unique_ids_num = cellfun(@str2num,unique(ids))
ids_num = cellfun(@str2num,ids)
counts = histc(ids_num,sort(unique_ids_num))
Cell_out = cell(numel(unique_ids_num),max(counts));
for k =1:numel(counts) %%// Maybe accumarray can work here
Cell_out(k,1:counts(k)) = Cell_in(ids_num==unique_ids_num(k));
end
<强>输出强>
Cell_out =
'airplane1' 'bat1' 'coal1'
'cat2' 'ball2' []
'ball3' 'cat3' 'doggie3'
答案 1 :(得分:1)
按照kyamagu的建议使用regexp
,然后accumarray
进行分组:
[~,~,ic] = unique(cell2mat(regexp(Cell_in(:), '\d+$', 'match', 'once')));
[ic,inds] = sort(ic); % to ensure stable ordering of output
co = accumarray(ic,inds,[],@(x){Cell_in(x)});
Cell_out = vertcat(co{:});