comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_mdist.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 Md = som_mdist(D,q,mask,Ne)
2
3 % SOM_MDIST Mutual (or pairwise) distance matrix for the given data.
4 %
5 % Md = som_mdist(D,[q],[mask],[Ne])
6 %
7 % Md = som_mdist(D);
8 % Md = som_mdist(D,Inf);
9 % Md = som_mdist(D,2,Ne);
10 %
11 % Input and output arguments ([]'s are optional):
12 % D (matrix) size dlen x dim, the data set
13 % (struct) map or data struct
14 % [q] (scalar) distance norm, default = 2
15 % [mask] (vector) size dim x 1, the weighting mask
16 % [Ne] (matrix) size dlen x dlen, sparse matrix
17 % indicating which distances should be
18 % calculated (ie. less than Infinite)
19 %
20 % See also PDIST.
21
22 % Copyright (c) 2000 by Juha Vesanto
23 % Contributed to SOM Toolbox on XXX by Juha Vesanto
24 % http://www.cis.hut.fi/projects/somtoolbox/
25
26 % Version 2.0beta juuso 220800
27
28 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29
30 % mask
31 if nargin<3, mask = []; end
32
33 % the data
34 if isstruct(D),
35 switch D.type,
36 case 'som_map', if isempty(mask), mask = D.mask; end, D = D.codebook;
37 case 'som_data', D = D.data;
38 otherwise, error('Bad first argument');
39 end
40 end
41 nans = sum(isnan(D),2);
42 if any(nans>0),
43 D(find(nans>0),:) = 0;
44 warning('Distances of vectors with NaNs are not calculated.');
45 end
46 [dlen dim] = size(D);
47
48 % distance norm
49 if nargin<2 | isempty(q) | isnan(q), q = 2; end
50
51 % mask
52 if isempty(mask), mask = ones(dim,1); end
53
54 % connections
55 if nargin<4, Ne = []; end
56 if ~isempty(Ne),
57 l = size(Ne,1); Ne([0:l-1]*l+[1:l]) = 1; % set diagonal elements = 1
58 end
59
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61
62 m = mask;
63 o = ones(dlen,1);
64 l = dlen;
65 Md = zeros(dlen);
66 calculate_all = isempty(Ne);
67
68 if ~calculate_all, Md(Ne==0) = Inf; end
69
70 for i=1:l-1,
71 j=(i+1):l;
72 if ~calculate_all, j=find(Ne(i,j))+i; end
73 C=D(j,:)-D(i*o(1:length(j)),:);
74 switch q,
75 case 1, Md(j,i)=abs(C)*m;
76 case 2, Md(j,i)=sqrt((C.^2)*m);
77 case Inf, Md(j,i)=max(diag(m)*abs(C),[],2);
78 otherwise, Md(j,i)=((abs(C).^q)*m).^(1/q);
79 end
80 Md(i,j) = Md(j,i)';
81 end
82
83 Md(find(nans>0),:) = NaN;
84 Md(:,find(nans>0)) = NaN;
85
86 return;
87
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89