function [AA1,C] = smartcollapse(AA,C) N = size(AA,1); T = size(AA,3); for t=1:T [h0,i0,j0] = SC(AA(:,:,t)); merge = setdiff(1:N,i0); tha = pnormdim(rand(N-1,1),1); AA1(:,:,t) = collapse(AA(:,:,t),merge,tha); end return function [h,i0,j0] = SC(AA) % the "stricture coefficient" of AA [n,n,T] = size(AA); h = 0; for t=1:T A = AA(:,:,t); for i=1:n for j=i+1:n x = A(:,i); y = A(:,j); hxy = max(abs(x-y)); if (hxy>h) i0=i;j0=j;h=hxy; end end end end return