-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp_get_angle_between_vectors.m
73 lines (68 loc) · 2.39 KB
/
sp_get_angle_between_vectors.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
function angle_deg = sp_get_angle_between_vectors(a,b)
% function angle_deg = sp_get_angle_between_vectors(a,b)
%
% Returns the angle between two vectors in degress
% If a,b are matrices, vectors are taken columwise
%
% Works for 2D and 3D cases
%
% INPUTS:
% a: [array] OR [matrix] with vectors in columns
% b: [array] OR [matrix] with vectors in columns
%
% OUTPUT:
% angle_deg: [array] of angles between vectors in degress
%
% Sagi Perel, 02/2013
if(nargin ~= 2)
error('sp_get_angle_between_vectors: wrong number of input arguments provided');
end
if(sp_isvector(a))
a_isvector = true;
a = make_column_vector(a);
dim = length(a);
elseif(sp_ismatrix(a))
a_isvector = false;
dim = size(a,1);
else
error('sp_get_angle_between_vectors: a must be a vector or a matrix');
end
if(sp_isvector(b))
if(~a_isvector)
error('sp_get_angle_between_vectors: a is a not a vecror, but b is. a,b must both be vectors or matrices');
end
b = make_column_vector(b);
elseif(sp_ismatrix(b))
if(a_isvector)
error('sp_get_angle_between_vectors: a is a vecror, but b is not. a,b must both be vectors or matrices');
end
if(~all( size(a) == size(b) ))
error('sp_get_angle_between_vectors: a and b are matrices with incompatible sizes');
end
else
error('sp_get_angle_between_vectors: b must be b vector or a matrix');
end
switch(dim)
case 2
if(a_isvector)
angle_deg = rad2deg( acos(dot(a./norm(a),b./norm(b))) );
else
num_vectors = size(a,2);
angle_deg = nan(num_vectors,1);
for i=1:num_vectors
angle_deg(i) = rad2deg( acos(dot(a(:,i)./norm(a(:,i)),b(:,i)./norm(b(:,i)))) );
end
end
case 3
if(a_isvector)
angle_deg = rad2deg( atan2(norm(cross(a,b)),dot(a,b)) );
else
num_vectors = size(a,2);
angle_deg = nan(num_vectors,1);
for i=1:num_vectors
angle_deg(i) = rad2deg( atan2(norm(cross(a(:,i),b(:,i))),dot(a(:,i),b(:,i))) );
end
end
otherwise
error();
end