view scripts/statistics/base/ranks.m @ 16447:e3b33a7530bc

improve encapsulation of history window object * history-dockwidget.h, history-dockwidget.cc (history_dock_widget::history_dock_widget): Set status tip here. Connect history_dock_widget::information signal to main_window::report_status_message. Connect history_dock_widget::command_double_clicked signal to main_window::handle_command_double_clicked. (history_dock_widget::connect_visibility_changed, history_dock_widget::focus, history_dock_widget::handle_visibility): New functions. * main-window.h, main-window.cc (main_window::history_window): Rename from _history_dock_widget. Don't use a pointer. Change all uses. (main_window::main_window): Initialize it here. (main_window::~main_window): Don't delete _history_dock_widget. (main_window::focus_history_window_signal): New signal. (main_window::focus_history_window): Rename from main_window::focus_command_history. Emit focus_history_window_signal instead of performing actions here. (main_window::handle_command_history_visible): Delete. (main_window::connect_visibility_changed): Call history_window.connect_visibility_changed instead of performing actions here. (main_window::construct): Don't create _history_dock_widget. Adapt signal/slot connections for new history_window object.
author John W. Eaton <jwe@octave.org>
date Sat, 06 Apr 2013 16:46:14 -0400
parents 5d3a684236b0
children
line wrap: on
line source

## Copyright (C) 1995-2012 Kurt Hornik
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {} ranks (@var{x}, @var{dim})
## Return the ranks of @var{x} along the first non-singleton dimension
## adjusted for ties.  If the optional argument @var{dim} is
## given, operate along this dimension.
## @seealso{spearman, kendall}
## @end deftypefn

## Author: KH <Kurt.Hornik@wu-wien.ac.at>
## Description: Compute ranks

## This code was rather ugly, since it didn't use sort due to the
## fact of how to deal with ties. Now it does use sort and its
## even uglier!!! At least it handles NDArrays..

function y = ranks (x, dim)

  if (nargin != 1 && nargin != 2)
    print_usage ();
  endif

  if (! (isnumeric (x) || islogical (x)))
    error ("ranks: X must be a numeric vector or matrix");
  endif

  nd = ndims (x);
  sz = size (x);
  if (nargin != 2)
    ## Find the first non-singleton dimension.
    (dim = find (sz > 1, 1)) || (dim = 1);
  else
    if (!(isscalar (dim) && dim == fix (dim))
        || !(1 <= dim && dim <= nd))
      error ("ranks: DIM must be an integer and a valid dimension");
    endif
  endif

  if (sz(dim) == 1)
    y = ones (sz);
  else
    ## The algorithm works only on dim = 1, so permute if necesary.
    if (dim != 1)
      perm = [1 : nd];
      perm(1) = dim;
      perm(dim) = 1;
      x = permute (x, perm);
    endif
    sz = size (x);
    infvec = -Inf ([1, sz(2 : end)]);
    [xs, xi] = sort (x);
    eq_el = find (diff ([xs; infvec]) == 0);
    if (isempty (eq_el))
      [eq_el, y] = sort (xi);
    else
      runs = setdiff (eq_el, eq_el+1);
      len = diff (find (diff ([Inf; eq_el; -Inf]) != 1)) + 1;
      [eq_el, y] = sort (xi);
      for i = 1 : length (runs)
        y (xi (runs (i) + [0:(len(i)-1)]) + floor (runs (i) ./ sz(1))
           * sz(1)) = eq_el(runs(i)) + (len(i) - 1) / 2;
      endfor
    endif
    if (dim != 1)
      y = permute (y, perm);
    endif
  endif

endfunction


%!assert (ranks (1:2:10), 1:5)
%!assert (ranks (10:-2:1), 5:-1:1)
%!assert (ranks ([2, 1, 2, 4]), [2.5, 1, 2.5, 4])
%!assert (ranks (ones (1, 5)), 3*ones (1, 5))
%!assert (ranks (1e6*ones (1, 5)), 3*ones (1, 5))
%!assert (ranks (rand (1, 5), 1), ones (1, 5))

%% Test input validation
%!error ranks ()
%!error ranks (1, 2, 3)
%!error ranks ({1, 2})
%!error ranks (['A'; 'B'])
%!error ranks (1, 1.5)
%!error ranks (1, 0)
%!error ranks (1, 3)