


zOrderbySimilarity uses the result of a cluster analysis and puts the instances in order, consistent with the tree, so that similar branches are near each other in the list



0001 % zOrderbySimilarity uses the result of a cluster analysis and puts the 0002 % instances in order, consistent with the tree, so that similar branches are 0003 % near each other in the list 0004 0005 % DD is a symmetric mutual distance matrix 0006 % q is a permutation 0007 0008 function [q] = zOrderbySimilarity(DD) 0009 0010 [s,t] = size(DD); 0011 0012 W = 2.^(-abs(ones(s,1)*(1:s) - (1:s)'*ones(1,s))); % weight matrix 0013 0014 % ----------------------------------------- Cluster analysis 0015 0016 D = full(DD); 0017 for i = 1:s, 0018 D(i,i) = 0; % set diagonal to zero 0019 end 0020 0021 Y = squareform(full(D)); % convert to a vector 0022 Z = linkage(Y,'average'); % compute cluster tree 0023 0024 % D is the square matrix giving mutual distances, with zero down the diagonal 0025 % Y is a vector, needed for the linkage function 0026 % Z is the linkage, telling what coalesces with what 0027 0028 % ----------------------------------------- Re-order the elements in groups 0029 0030 q = 1:s; % simplest ordering, doesn't do well 0031 0032 for i = 1:s, 0033 Group{i} = i; % initial groups have one element 0034 end 0035 0036 gc = s; % group counter 0037 0038 for z = 1:length(Z(:,1)), 0039 g = Group{Z(z,1)}; 0040 h = Group{Z(z,2)}; 0041 gg = fliplr(g); 0042 0043 S(1) = Score(D([g h],[g h]),W); 0044 S(2) = Score(D([h g],[h g]),W); 0045 S(3) = Score(D([gg h],[gg h]),W); 0046 S(4) = Score(D([h gg],[h gg]),W); 0047 0048 [y,i] = sort(S); 0049 0050 switch i(1) 0051 case 1, Group{gc+1} = [g h]; 0052 case 2, Group{gc+1} = [h g]; 0053 case 3, Group{gc+1} = [gg h]; 0054 case 4, Group{gc+1} = [h gg]; 0055 end 0056 0057 gc = gc + 1; 0058 end 0059 0060 q = Group{end}; 0061 0062 % ----------------------------------------------------------------------- 0063 % The Score function tells which type of distance matrix is preferred. 0064 % This simple scoring scheme prefers small values near the diagonal. 0065 0066 function [S] = Score(M,W) 0067 0068 % S = 2*sum(diag(M,1)) + sum(diag(M,2)); 0069 0070 [s,t] = size(M); 0071 S = sum(sum(M .* W(1:s,1:t))); 0072