changeset 27761:bd40796683d8

rescale.m: New function (bug #56639). * NEWS: Announce new function. * scripts/general/rescale.m: New function. * scripts/general/module.mk: Add function to build system. * io.txi: Add function to Octave manual. * contributors.in: Add Christian Himpe to list of contributors.
author Christian Himpe <christian.himpe@wwu.de>
date Sat, 30 Nov 2019 07:22:46 -0800
parents b70da79e4ed9
children 1a8fd58af587
files NEWS doc/interpreter/contributors.in doc/interpreter/io.txi scripts/general/module.mk scripts/general/rescale.m
diffstat 5 files changed, 117 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri Nov 29 10:41:03 2019 +0100
+++ b/NEWS	Sat Nov 30 07:22:46 2019 -0800
@@ -136,6 +136,7 @@
 * `lightangle`
 * `namedargs2cell`
 * `newline`
+* `rescale`
 * `rotx`
 * `roty`
 * `rotz`
--- a/doc/interpreter/contributors.in	Fri Nov 29 10:41:03 2019 +0100
+++ b/doc/interpreter/contributors.in	Sat Nov 30 07:22:46 2019 -0800
@@ -147,6 +147,7 @@
 Jordi GutiƩrrez Hermoso
 Israel Herraiz
 Yozo Hida
+Christian Himpe
 Ryan Hinton
 Roman Hodek
 A. Scottedward Hodel
--- a/doc/interpreter/io.txi	Fri Nov 29 10:41:03 2019 +0100
+++ b/doc/interpreter/io.txi	Sat Nov 30 07:22:46 2019 -0800
@@ -233,6 +233,10 @@
 
 @DOCSTRING(importdata)
 
+After importing, the data may need to be transformed before further analysis.  The @code{rescale} function can shift and normalize a data set in to a specified range.
+
+@DOCSTRING(rescale)
+
 @menu
 * Saving Data on Unexpected Exits::
 @end menu
--- a/scripts/general/module.mk	Fri Nov 29 10:41:03 2019 +0100
+++ b/scripts/general/module.mk	Sat Nov 30 07:22:46 2019 -0800
@@ -59,6 +59,7 @@
   %reldir%/rat.m \
   %reldir%/repelem.m \
   %reldir%/repmat.m \
+  %reldir%/rescale.m \
   %reldir%/rot90.m \
   %reldir%/rotdim.m \
   %reldir%/shift.m \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/general/rescale.m	Sat Nov 30 07:22:46 2019 -0800
@@ -0,0 +1,110 @@
+## Copyright (C) 2019 Christian Himpe
+##
+## 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
+## <https://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn  {} {} rescale (@var{A})
+## @deftypefnx {} {} rescale (@var{A}, @var{l}, @var{u})
+## @deftypefnx {} {} rescale (@var{A}, 'inputmin', @var{inmin})
+## @deftypefnx {} {} rescale (@var{A}, 'inputmax', @var{inmax})
+## @deftypefnx {} {} rescale (@var{A}, 'inputmin', @var{inmin}, 'inputmax', @var{inmax})
+## @deftypefnx {} {} rescale (@var{A}, @var{l}, @var{u}, 'inputmin', @var{inmin})
+## @deftypefnx {} {} rescale (@var{A}, @var{l}, @var{u}, 'inputmax', @var{inmax})
+## @deftypefnx {} {} rescale (@var{A}, @var{l}, @var{u}, 'inputmin', @var{inmin}, 'inputmax', @var{inmax})
+## Rescale matrix.
+##
+## Rescales a matrix to the interval [l,u]. The name-value pairs 'inputmin' and
+## 'inputmax' set all elements of A to min(A,inmin) and max(A,inmax)
+## respectively. The applied formula is:
+## @tex
+## B = l + \frac{A - inmin}{inmax - inmin} * (u - l).
+## @end tex
+##
+## The default value for the lower bound @var{l} is 0, and for the upper bound
+## @var{u} is 1. If the name-value pairs 'inputmin' or 'inputmax' are not set,
+## the minimum and maximum input range are set to @var{inmin} = min(A(:)) and
+## @var{inmax) = max(A(:)) respectively.
+##
+## @seealso{min, max}
+## @end deftypefn
+
+## Author: Christian Himpe <christian.himpe@wwu.de>
+## Created: November 2019
+
+function B = rescale(A,varargin)
+
+    % Check if 1st argument is a matrix
+    if not(isnumeric(A)) || not(any(nargin == [1,3,5,7]))
+
+        print_usage();
+    endif
+
+    l = 0;
+    u = 1.0;
+
+    % Check if 2nd and 3rd argument are numeric, then set non-default interval.
+    if nargin > 1 && isnumeric(varargin{1})
+
+        l = varargin{1}; 
+
+        if isnumeric(varargin{2})
+
+            u = varargin{2};
+        else
+
+            print_usage();
+        endif
+    endif
+
+    % Check for named argument 'inputmin'.
+    if not(isempty(varargin))
+
+        inminidx = find(strcmp(lower(varargin),'inputmin'));
+    else
+
+        inminidx = [];
+    endif
+        
+    if not(isempty(inminidx)) && isnumeric(varargin(inminidx(end) + 1))
+
+        inmin = varargin(inminidx(end) + 1);
+    else
+
+        inmin = min(A(:));
+    endif
+
+    % Check for named argument 'inputmax'.
+    if not(isempty(varargin))
+
+        inmaxidx = find(strcmp(lower(varargin),'inputmax'));
+    else
+
+        inmaxidx = [];
+    endif
+        
+    if not(isempty(inmaxidx)) && isnumeric(varargin(inmaxidx(end) + 1));
+
+        inmax = varargin(inmaxidx(end) + 1);
+    else
+
+        inmax = max(A(:));
+    endif  
+
+    % Rescale A to interval [l,u] in range interval [inmin,inmax].
+    B = l + ( (A - inmin) ./ (inmax - inmin) ) .* (u - l);
+
+endfunction