forked from circstat/circstat-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circ_median.m
72 lines (61 loc) · 1.37 KB
/
circ_median.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
function med = circ_median(alpha,dim)
%
% med = circ_median(alpha)
% Computes the median direction for circular data.
%
% Input:
% alpha sample of angles in radians
% [dim compute along this dimension, default is 1, must
% be either 1 or 2 for circ_median]
%
% Output:
% mu median direction
%
% circ_median can be slow for large datasets
%
% Update 2012
% PHB 3/19/2009
%
% References:
% Biostatistical Analysis, J. H. Zar (26.6)
%
% Circular Statistics Toolbox for Matlab
% By Philipp Berens, 2009
% [email protected] - www.kyb.mpg.de/~berens/circStat.html
if nargin < 2
dim = 1;
end
M = size(alpha);
med = NaN(M(3-dim),1);
for i=1:M(3-dim)
if dim == 2
beta = alpha(i,:)';
elseif dim ==1
beta = alpha(:,i);
else
error('circ_median only works along first two dimensions')
end
beta = mod(beta,2*pi);
n = size(beta,1);
dd = circ_dist2(beta,beta);
m1 = sum(dd>=0,1);
m2 = sum(dd<=0,1);
dm = abs(m1-m2);
if mod(n,2)==1
[m idx] = min(dm);
else
m = min(dm);
idx = find(dm==m,2);
end
if m > 1
warning('Ties detected.') %#ok<WNTAG>
end
md = circ_mean(beta(idx));
if abs(circ_dist(circ_mean(beta),md)) > abs(circ_dist(circ_mean(beta),md+pi))
md = mod(md+pi,2*pi);
end
med(i) = md;
end
if dim == 2
med = med';
end