-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsangle.m
57 lines (46 loc) · 1.35 KB
/
sangle.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
function s_a = sangle(X, varargin)
% Calculate spectral angle
%
% s_a(x,y) = <x,y>/(||x||_2 * ||y||_2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Parse inputs
if nargin > 1
compare_mode = 'cross';
Y = varargin{1};
if size(X, 2) ~= size(Y,2)
error('Number of vectors must be the same!')
end
if size(X, 1) ~= size(Y,1)
error('Size of vectors must be the same!')
end
else
compare_mode = 'self';
Y = [];
end
if nargin > 2
comp_type = varargin{2};
else
comp_type = 'vec';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Compute spectral angles
if strcmp(compare_mode, 'self')
% num_vec = size(X, 2);
norm_vals = sqrt(sum(X.^2, 1));
inner_prod = (X')*X;
s_a = acosd(inner_prod./((norm_vals')*norm_vals));
elseif strcmp(compare_mode, 'cross')
norm_X = sqrt(sum(X.^2, 1));
norm_Y = sqrt(sum(Y.^2, 1));
if strcmp(comp_type, 'vec')
inner_prod = sum((Y).*X, 1);
s_a = acosd(inner_prod./((norm_X).*norm_Y));
else
inner_prod = (Y')*X;
s_a = acosd(inner_prod./((norm_X')*norm_Y));
end
else
error('Bad comparison mode!')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%