sortcell.m

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· M 代码 · 共 37 行

M
37
字号
function [T, perm] = sortcell(T)% SORTCELL Sort elements of a 1D cell array first by length, then in ASCII order% [T, perm] = sortcell(T)%% Example% [T, perm] = sortcell({[2 3], [1 2], 1})% T = { [1], [1 2], [2 3] }% perm = [3 2 1]% first sort by lengthm = length(T);L = zeros(1,m);for s=1:m  L(s) = length(T{s});end[L, perm] = sort(L);T = T(perm);% now sort each block of same lengthU = unique(L);h = hist(L, 1:max(L)); % how many entries of each lengthH = cumsum(h);for i=1:length(H)  if i==1    ndx = 1:H(1);  else    ndx = H(i-1)+1:H(i);  end  M = T(ndx);  M = cat(1, M{:});  [M, perm2] = sortrows(M);  T(ndx) = num2cell(M,2);  p = perm(ndx);  perm(ndx) = p(perm2);end  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?