forked from ilarinieminen/SOM-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsom_eucdist2.m
92 lines (78 loc) · 2.21 KB
/
som_eucdist2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function d=som_eucdist2(Data, Proto)
%SOM_EUCDIST2 Calculates matrix of squared euclidean distances between set of vectors or map, data struct
%
% d=som_eucdist2(D, P)
%
% d=som_eucdist(sMap, sData);
% d=som_eucdist(sData, sMap);
% d=som_eucdist(sMap1, sMap2);
% d=som_eucdist(datamatrix1, datamatrix2);
%
% Input and output arguments ([]'s are optional):
% D (matrix) size Nxd
% (struct) map or data struct
% P (matrix) size Pxd
% (struct) map or data struct
% d (matrix) distance matrix of size NxP
%
% IMPORTANT
%
% * Calculates _squared_ euclidean distances
% * Observe that the mask in the map struct is not taken into account while
% calculating the euclidean distance
%
% See also KNN, PDIST.
% Contributed to SOM Toolbox 2.0, October 29th, 2000 by Johan Himberg
% Copyright (c) by Johan Himberg
% http://www.cis.hut.fi/projects/somtoolbox/
% Version 2.0beta Johan 291000
%% Init %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isstruct(Data);
if isfield(Data,'type') && ischar(Data.type)
else
error('Invalid map/data struct?');
end
switch Data.type
case 'som_map'
data=Data.codebook;
case 'som_data'
data=Data.data;
end
else
% is already a matrix
data=Data;
end
% Take prototype vectors from prototype struct
if isstruct(Proto),
if isfield(Proto,'type') && ischar(Proto.type),
else
error('Invalid map/data struct?');
end
switch Proto.type
case 'som_map'
proto=Proto.codebook;
case 'som_data'
proto=Proto.data;
end
else
% is already a matrix
proto=Proto;
end
% Check that inputs are matrices
if ~vis_valuetype(proto,{'nxm'}) || ~vis_valuetype(data,{'nxm'}),
error('Prototype or data input not valid.')
end
% Record data&proto sizes and check their dims
[N_data dim_data]=size(data);
[N_proto dim_proto]=size(proto);
if dim_proto ~= dim_data,
error('Data and prototype vector dimension does not match.');
end
% Calculate euclidean distances between classifiees and prototypes
d=distance(data,proto);
%%%% Classification %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function d=distance(X,Y);
% Euclidean distance matrix between row vectors in X and Y
U=~isnan(Y); Y(~U)=0;
V=~isnan(X); X(~V)=0;
d=abs(X.^2*U'+V*Y'.^2-2*X*Y');