home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / DATAFUN.DI$ / MEDIAN.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  792 b   |  31 lines

  1. function y = median(x)
  2. %MEDIAN    Median value.
  3. %    For vectors,  MEDIAN(X)  is the median value of the elements in X.
  4. %    For matrices, MEDIAN(X) is a row vector containing the median value
  5. %    of each column.
  6. %
  7. %    See also MEAN, STD, MAX, MIN.
  8.  
  9. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  10. [m,n] = size(x);
  11. x = sort(x);
  12. if (m==1)
  13.     if isnan(x(1)) | isnan(x(n))
  14.         y = nan;
  15.         return
  16.     end
  17.     if rem(n,2)        % n is odd    
  18.         y = x((n+1)/2);
  19.     else            % n is even
  20.         y = (x(n/2) + x(n/2+1))/2;
  21.     end
  22. else
  23.     if rem(m,2)        % m is odd
  24.         y = x((m+1)/2,:);
  25.     else            % m is even
  26.         y = (x(m/2,:) + x(m/2+1,:))/2;
  27.     end
  28.     nn = find( isnan(x(1,:)) | isnan(x(m,:)) );
  29.     y(nn) = nan*ones(size(nn));
  30. end
  31.