# HG changeset patch # User jwe # Date 1071631889 0 # Node ID c0302db81b7502484c63f220a9ac35db81fe3ec9 # Parent e1c2d8ca8bc00af1c6217f4299aa36458c3fd66a [project @ 2003-12-17 03:31:29 by jwe] diff -r e1c2d8ca8bc0 -r c0302db81b75 scripts/general/ind2sub.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/general/ind2sub.m Wed Dec 17 03:31:29 2003 +0000 @@ -0,0 +1,75 @@ +## Copyright (C) 2001 Paul Kienzle +## +## 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 2, 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, write to the Free +## Software Foundation, 59 Temple Place - Suite 330, Boston, MA +## 02111-1307, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {[@var{s1}, @var{s2}, @dots{}, @var{sN}] =} sub2ind (@var{dims}, @var{ind}) +## Convert a linear index into subscripts. +## @end deftypefn +## @seealso{sub2ind} + +## Author: Paul Kienzle +## Adapted-by: jwe + +function varargout = ind2sub (dims, ind) + + if (nargin == 2) + if (isvector (dims) && round (dims) == dims) + if (isnumeric (ind) && round (ind) == ind) + ntot = prod (dims); + if (ind > 0 & ind <= ntot) + nd = length (dims); + if (nargout > 0) + vlen = nargout; + else + vlen = 1; + endif + if (nd > vlen); + dims(vlen) = prod (dims(vlen:nd)); + dims(vlen+1:nd) = []; + endif + nd = length (dims); + scale = [1; cumprod(dims(:))]; + for i = nd:-1:2 + k = (ind >= scale(i)); + r = ones (size (ind)); + t = zeros (size (ind)); + t(k) = floor ((ind(k) - 1) / scale(i)); + r(k) = t(k) + 1; + varargout{i} = r; + ind(k) -= t(k) * scale(i); + endfor + varargout{1} = ind; + for i = nd+1:vlen + varargout{i} = ones (size (ind)); + endfor + else + error ("ind2sub: index out of range"); + endif + else + error ("ind2sub: expecting integer-valued index argument"); + endif + else + error ("sub2ind: expecting dims to be an integer vector"); + endif + else + usage ("ind2sub (dims, ind)"); + endif + + +endfunction diff -r e1c2d8ca8bc0 -r c0302db81b75 scripts/general/sub2ind.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/general/sub2ind.m Wed Dec 17 03:31:29 2003 +0000 @@ -0,0 +1,71 @@ +## Copyright (C) 2001 Paul Kienzle +## +## 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 2, 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, write to the Free +## Software Foundation, 59 Temple Place - Suite 330, Boston, MA +## 02111-1307, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{ind} =} sub2ind (@var{dims}, @var{i}, @var{j}) +## @deftypefnx {Function File} {@var{ind} =} sub2ind (@var{dims}, @var{s1}, @var{s2}, @dots{}, @var{sN}) +## Convert subscripts into a linear index. +## @end deftypefn +## @seealso{ind2sub} + +## Author: Paul Kienzle +## Adapted-by: jwe + +function ind = sub2ind (dims, varargin) + + if (nargin > 1) + if (isvector (dims) && round (dims) == dims) + nd = length (dims); + vlen = length (varargin); + dims(vlen) = prod (dims(vlen:nd)); + dims(vlen+1:nd) = []; + scale = cumprod (dims(:)); + for i = 1:vlen + arg = varargin{i}; + if (isnumeric (arg) && round (arg) == arg) + if (i == 1) + if (arg > 0 & arg <= dims(i)) + ind = first_arg = arg; + else + error ("sub2ind: index out of range"); + endif + else + if (all (size (first_arg) == size (arg))) + if ((i > nd && arg == 1) || (arg > 0 & arg <= dims(i))) + ind += scale(i-1) * (arg - 1); + else + error ("sub2ind: index out of range"); + endif + else + error ("sub2ind: all index arguments must be the same size"); + endif + endif + else + error ("sub2ind: expecting integer-valued index arguments"); + endif + endfor + else + error ("sub2ind: expecting dims to be an integer vector"); + endif + else + usage ("sub2ind (dims, i1, i2, ..., iN)"); + endif + + +endfunction