Mercurial > hg > octave-lyh
annotate scripts/general/ind2sub.m @ 9163:9cb0c21e97f7
Update section 17.4 (Sums and Products) of arith.txi
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Thu, 30 Apr 2009 16:13:18 -0700 |
parents | 1bf0ce0930be |
children | 16f53d29049f |
rev | line source |
---|---|
7345 | 1 ## Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007, 2008 Paul Kienzle |
4679 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
4679 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
4679 | 18 |
19 ## -*- texinfo -*- | |
4906 | 20 ## @deftypefn {Function File} {[@var{s1}, @var{s2}, @dots{}, @var{sN}] =} ind2sub (@var{dims}, @var{ind}) |
4679 | 21 ## Convert a linear index into subscripts. |
6631 | 22 ## |
23 ## The following example shows how to convert the linear index @code{8} | |
7345 | 24 ## in a 3-by-3 matrix into a subscript. The matrix is linearly indexed |
25 ## moving from one column to next, filling up all rows in each column. | |
6631 | 26 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7345
diff
changeset
|
27 ## @group |
6631 | 28 ## [r, c] = ind2sub ([3, 3], 8) |
29 ## @result{} r = 2 | |
30 ## c = 3 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7345
diff
changeset
|
31 ## @end group |
6631 | 32 ## @end example |
5642 | 33 ## @seealso{sub2ind} |
4679 | 34 ## @end deftypefn |
35 | |
36 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> | |
37 ## Adapted-by: jwe | |
38 | |
39 function varargout = ind2sub (dims, ind) | |
40 | |
41 if (nargin == 2) | |
6050 | 42 if (isvector (dims) && all (round (dims) == dims)) |
43 if (isnumeric (ind) && all (round (ind) == ind)) | |
4679 | 44 ntot = prod (dims); |
6050 | 45 if (all (ind > 0 & ind <= ntot)) |
4679 | 46 nd = length (dims); |
47 if (nargout > 0) | |
48 vlen = nargout; | |
49 else | |
50 vlen = 1; | |
51 endif | |
52 if (nd > vlen); | |
53 dims(vlen) = prod (dims(vlen:nd)); | |
54 dims(vlen+1:nd) = []; | |
55 endif | |
56 nd = length (dims); | |
57 scale = [1; cumprod(dims(:))]; | |
58 for i = nd:-1:2 | |
59 k = (ind >= scale(i)); | |
60 r = ones (size (ind)); | |
61 t = zeros (size (ind)); | |
62 t(k) = floor ((ind(k) - 1) / scale(i)); | |
63 r(k) = t(k) + 1; | |
64 varargout{i} = r; | |
65 ind(k) -= t(k) * scale(i); | |
66 endfor | |
67 varargout{1} = ind; | |
68 for i = nd+1:vlen | |
69 varargout{i} = ones (size (ind)); | |
70 endfor | |
71 else | |
72 error ("ind2sub: index out of range"); | |
73 endif | |
74 else | |
75 error ("ind2sub: expecting integer-valued index argument"); | |
76 endif | |
77 else | |
5518 | 78 error ("ind2sub: expecting dims to be an integer vector"); |
4679 | 79 endif |
80 else | |
6046 | 81 print_usage (); |
4679 | 82 endif |
83 | |
84 | |
85 endfunction |