comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_cldist.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function Cd = som_cldist(D,clinds1,clinds2,cldist,q,mask)
2
3 % SOM_CLDIST Distances between two clusters.
4 %
5 % Cd = som_cldist(Md,c1,c2,'single')
6 % Cd = som_cldist(Md,c1,c2,'average')
7 % Cd = som_cldist(Md,c1,c2,'complete')
8 % Cd = som_cldist(Md,c1,c2,'neighf',H)
9 % Cd = som_cldist(Md,c1,[],...)
10 % Cd = som_cldist(D,c1,c2,'centroid',q,mask)
11 % Cd = som_cldist(D,c1,c2,'ward',q,mask)
12 % Cd = som_cldist(D,c1,[],...)
13 %
14 % Input and output arguments ([]'s are optional):
15 % D (matrix) size dlen x dim, the data set
16 % (struct) map or data struct
17 % Md (matrix) size dlen x dlen, mutual distance matrix, see SOM_MDIST
18 % c1 (cell array) size n1 x 1, indices of clusters from which
19 % the distances should be calculated, each cell
20 % contains indices of vectors that belong to that
21 % cluster (indices are between 1...dlen)
22 % c2 (cell array) size n2 x 1, same as c1 but have the clusters
23 % to which the distances should be calculated
24 % (empty) c1 is used in place of c2
25 % [q] (scalar) distance norm, default = 2
26 % [mask] (vector) size dim x 1, the weighting mask, a vector of ones
27 % by default
28 % H (matrix) size dlen x dlen, neighborhood function values
29 %
30 % Cd (matrix) size n1 x n2, distances between the clusters
31 %
32 % See also SOM_MDIST.
33
34 % Copyright (c) 2000 by Juha Vesanto
35 % Contributed to SOM Toolbox on XXX by Juha Vesanto
36 % http://www.cis.hut.fi/projects/somtoolbox/
37
38 % Version 2.0beta juuso 250800
39
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41
42 [dlen dim] = size(D);
43 if nargin<5, q = 2; end
44 if nargin<6, mask = ones(dim,1); end
45 if ~iscell(clinds1), clinds1 = {clinds1}; end
46 if ~isempty(clinds2) & ~iscell(clinds2), clinds2 = {clinds2}; end
47
48 n1 = length(clinds1);
49 n2 = length(clinds2);
50 if n2>0, Cd = zeros(n1,n2); else Cd = zeros(n1); end
51 if n1==0, return; end
52
53 switch cldist,
54
55 % centroid distance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 case 'centroid',
57
58 C1 = zeros(n1,dim); for i=1:n1, C1(i,:) = mean(D(clinds1{i},:),1); end
59 C2 = zeros(n2,dim); for i=1:n2, C2(i,:) = mean(D(clinds2{i},:),1); end
60 if n2==0,
61 for i=1:n1-1,
62 for j=i+1:n1,
63 diff = C1(i,:)-C1(j,:);
64 switch q,
65 case 1, Cd(i,j)=abs(diff)*mask;
66 case 2, Cd(i,j)=sqrt((diff.^2)*mask);
67 case Inf, Cd(i,j)=max(diag(mask)*abs(diff),[],2);
68 otherwise, Cd(i,j)=((abs(diff).^q)*mask).^(1/q);
69 end
70 end
71 Cd([(i+1):n1],i) = Cd(i,[(i+1):n1])';
72 end
73 else
74 for i=1:n1,
75 for j=1:n2,
76 diff = C1(i,:)-C2(j,:);
77 switch q,
78 case 1, Cd(i,j)=abs(diff)*mask;
79 case 2, Cd(i,j)=sqrt((diff.^2)*mask);
80 case Inf, Cd(i,j)=max(diag(mask)*abs(diff),[],2);
81 otherwise, Cd(i,j)=((abs(diff).^q)*mask).^(1/q);
82 end
83 end
84 end
85 end
86
87 % ward distance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 case 'ward',
89
90 C1 = zeros(n1,dim); nn1 = zeros(n1,dim);
91 for i=1:n1, C1(i,:) = mean(D(clinds1{i},:),1); nn1(i) = length(clinds1{i}); end
92 C2 = zeros(n2,dim); nn2 = zeros(n2,dim);
93 for i=1:n2, C2(i,:) = mean(D(clinds2{i},:),1); nn2(i) = length(clinds2{i}); end
94 if n2==0,
95 for i=1:n1-1,
96 for j=i+1:n1,
97 diff = C1(i,:) - C1(j,:);
98 f = 2*nn1(i)*nn1(j) / (nn1(i)+nn1(j));
99 switch q,
100 case 1, Cd(i,j)=f*abs(diff)*mask;
101 case 2, Cd(i,j)=f*sqrt((diff.^2)*mask);
102 case Inf, Cd(i,j)=f*max(diag(mask)*abs(diff),[],2);
103 otherwise, Cd(i,j)=f*((abs(diff).^q)*mask).^(1/q);
104 end
105 end
106 Cd([(i+1):n1],i) = Cd(i,[(i+1):n1])';
107 end
108 else
109 for i=1:n1,
110 for j=1:n2,
111 diff = C1(i,:) - C2(j,:);
112 f = 2*nn1(i)*nn2(j) / (nn1(i)+nn2(j));
113 switch q,
114 case 1, Cd(i,j)=f*abs(diff)*mask;
115 case 2, Cd(i,j)=f*sqrt((diff.^2)*mask);
116 case Inf, Cd(i,j)=f*max(diag(mask)*abs(diff),[],2);
117 otherwise, Cd(i,j)=f*((abs(diff).^q)*mask).^(1/q);
118 end
119 end
120 end
121 end
122
123 % single linkage distance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 case 'single',
125
126 if n2==0,
127 for i=1:n1-1,
128 for j=i+1:n1,
129 vd = D(clinds1{i},clinds1{j});
130 fi = isfinite(vd(:));
131 if any(fi), Cd(i,j) = min(vd(fi)); else Cd(i,j) = Inf; end
132 end
133 Cd([(i+1):n1],i) = Cd(i,[(i+1):n1])';
134 end
135 else
136 for i=1:n1,
137 for j=1:n2,
138 vd = D(clinds1{i},clinds2{j});
139 fi = isfinite(vd(:));
140 if any(fi), Cd(i,j) = min(vd(fi)); else Cd(i,j) = Inf; end
141 end
142 end
143 end
144
145 % average linkage distance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 case 'average',
147
148 if n2==0,
149 for i=1:n1-1,
150 for j=i+1:n1,
151 vd = D(clinds1{i},clinds1{j});
152 fi = isfinite(vd(:));
153 if any(fi), Cd(i,j) = mean(vd(fi)); else Cd(i,j) = Inf; end
154 end
155 Cd([(i+1):n1],i) = Cd(i,[(i+1):n1])';
156 end
157 else
158 for i=1:n1,
159 for j=1:n2,
160 vd = D(clinds1{i},clinds2{j});
161 fi = isfinite(vd(:));
162 if any(fi), Cd(i,j) = mean(vd(fi)); else Cd(i,j) = Inf; end
163 end
164 end
165 end
166
167 % complete linkage distance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 case 'complete',
169
170 if n2==0,
171 for i=1:n1-1,
172 for j=i+1:n1,
173 vd = D(clinds1{i},clinds1{j});
174 fi = isfinite(vd(:));
175 if any(fi), Cd(i,j) = max(vd(fi)); else Cd(i,j) = Inf; end
176 end
177 Cd([(i+1):n1],i) = Cd(i,[(i+1):n1])';
178 end
179 else
180 for i=1:n1,
181 for j=1:n2,
182 vd = D(clinds1{i},clinds2{j});
183 fi = isfinite(vd(:));
184 if any(fi), Cd(i,j) = max(vd(fi)); else Cd(i,j) = Inf; end
185 end
186 end
187 end
188
189 % neighborhood function linkage distance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 case 'neighf',
191
192 if n2==0,
193 for i=1:n1-1,
194 for j=i+1:n1,
195 vd = D(clinds1{i},clinds1{j});
196 fi = isfinite(vd(:));
197 if any(fi),
198 hd = q(clinds1{i},clinds1{j});
199 hd = hd(fi);
200 Cd(i,j) = sum(hd.*vd(fi))/sum(hd);
201 else Cd(i,j) = Inf;
202 end
203 end
204 Cd([(i+1):n1],i) = Cd(i,[(i+1):n1])';
205 end
206 else
207 for i=1:n1,
208 for j=1:n2,
209 vd = D(clinds1{i},clinds2{j});
210 fi = isfinite(vd(:));
211 if any(fi),
212 hd = q(clinds1{i},clinds2{j});
213 hd = hd(fi);
214 Cd(i,j) = sum(hd.*vd(fi))/sum(hd);
215 else Cd(i,j) = Inf;
216 end
217 end
218 end
219 end
220
221 otherwise, error(['Unknown cluster distance metric: ' cldist]);
222 end
223
224 return;
225
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227