Home > atmlab > circular > circ_median.m

circ_median

PURPOSE ^

SYNOPSIS ^

function med = circ_median(alpha,dim)

DESCRIPTION ^

 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        mean direction

 PHB 3/19/2009

 References:
   Biostatistical Analysis, J. H. Zar (26.6)

 Circular Statistics Toolbox for Matlab

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

circ_median.m

SOURCE CODE ^

0001 function med = circ_median(alpha,dim)
0002 %
0003 % med = circ_median(alpha)
0004 %   Computes the median direction for circular data.
0005 %
0006 %   Input:
0007 %     alpha    sample of angles in radians
0008 %     [dim  compute along this dimension, default is 1, must
0009 %           be either 1 or 2 for circ_median]
0010 %
0011 %   Output:
0012 %     mu        mean direction
0013 %
0014 % PHB 3/19/2009
0015 %
0016 % References:
0017 %   Biostatistical Analysis, J. H. Zar (26.6)
0018 %
0019 % Circular Statistics Toolbox for Matlab
0020 
0021 % By Philipp Berens, 2009
0022 % berens@tuebingen.mpg.de - www.kyb.mpg.de/~berens/circStat.html
0023 
0024 if nargin < 2
0025   dim = 1;
0026 end
0027 
0028 M = size(alpha);
0029 for i=1:M(3-dim)
0030   if dim == 2
0031     beta = alpha(i,:)';
0032   elseif dim ==1
0033     beta = alpha(:,i);
0034   else
0035     error('circ_median only works along first two dimensions')
0036   end
0037   
0038   beta = mod(beta,2*pi);
0039   n = size(beta,1);
0040 
0041   m1 = sum(circ_dist2(beta,beta)>0,1);
0042   m2 = sum(circ_dist2(beta,beta)<0,1);
0043 
0044   dm = abs(m1-m2);
0045   if mod(n,2)==1
0046     [m idx] = min(dm);
0047   else
0048     m = min(dm);
0049     idx = find(dm==m,2);
0050   end
0051 
0052   if m > 1
0053     warning('Ties detected.') %#ok<WNTAG>
0054   end
0055 
0056   md = circ_mean(beta(idx));
0057 
0058   if abs(circ_dist(circ_mean(beta),md)) > abs(circ_dist(circ_mean(beta),md+pi))
0059     md = mod(md+pi,2*pi);
0060   end
0061   
0062   med(i) = md;
0063 end
0064 
0065 if dim == 2
0066   med = med';
0067 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005