Mercurial > hg > octave-lyh
changeset 4679:c0302db81b75
[project @ 2003-12-17 03:31:29 by jwe]
author | jwe |
---|---|
date | Wed, 17 Dec 2003 03:31:29 +0000 |
parents | e1c2d8ca8bc0 |
children | 493ff0643644 |
files | scripts/general/ind2sub.m scripts/general/sub2ind.m |
diffstat | 2 files changed, 146 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/scripts/general/ind2sub.m @@ -0,0 +1,75 @@ +## Copyright (C) 2001 Paul Kienzle <pkienzle@kienzle.powernet.co.uk> +## +## 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 <pkienzle@kienzle.powernet.co.uk> +## 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
new file mode 100644 --- /dev/null +++ b/scripts/general/sub2ind.m @@ -0,0 +1,71 @@ +## Copyright (C) 2001 Paul Kienzle <pkienzle@kienzle.powernet.co.uk> +## +## 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 <pkienzle@kienzle.powernet.co.uk> +## 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