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