Mercurial > hg > octave-lyh
annotate scripts/general/sub2ind.m @ 9207:25f50d2d76b3
improve TR updating strategy for fminunc and fsolve
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sun, 17 May 2009 17:54:51 +0200 |
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 -*- | |
20 ## @deftypefn {Function File} {@var{ind} =} sub2ind (@var{dims}, @var{i}, @var{j}) | |
21 ## @deftypefnx {Function File} {@var{ind} =} sub2ind (@var{dims}, @var{s1}, @var{s2}, @dots{}, @var{sN}) | |
22 ## Convert subscripts into a linear index. | |
6631 | 23 ## |
24 ## The following example shows how to convert the two-dimensional | |
7345 | 25 ## index @code{(2,3)} of a 3-by-3 matrix to a linear index. The matrix |
26 ## is linearly indexed moving from one column to next, filling up | |
27 ## all rows in each column. | |
6631 | 28 ## |
29 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7345
diff
changeset
|
30 ## @group |
6631 | 31 ## linear_index = sub2ind ([3, 3], 2, 3) |
32 ## @result{} 8 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7345
diff
changeset
|
33 ## @end group |
6631 | 34 ## @end example |
5642 | 35 ## @seealso{ind2sub} |
4679 | 36 ## @end deftypefn |
37 | |
38 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> | |
39 ## Adapted-by: jwe | |
40 | |
41 function ind = sub2ind (dims, varargin) | |
42 | |
43 if (nargin > 1) | |
6050 | 44 if (isvector (dims) && all (round (dims) == dims)) |
4679 | 45 nd = length (dims); |
46 vlen = length (varargin); | |
47 dims(vlen) = prod (dims(vlen:nd)); | |
48 dims(vlen+1:nd) = []; | |
49 scale = cumprod (dims(:)); | |
50 for i = 1:vlen | |
51 arg = varargin{i}; | |
6597 | 52 if (isnumeric (arg) && isequal (round (arg), arg)) |
4679 | 53 if (i == 1) |
6597 | 54 if (all (arg(:) > 0 & arg(:) <= dims(i))) |
4679 | 55 ind = first_arg = arg; |
56 else | |
57 error ("sub2ind: index out of range"); | |
58 endif | |
59 else | |
6597 | 60 if (size_equal (first_arg, arg)) |
61 if ((i > nd && arg == 1) || all (arg(:) > 0 & arg(:) <= dims(i))) | |
62 ind += scale(i-1) * (arg - 1); | |
4679 | 63 else |
64 error ("sub2ind: index out of range"); | |
65 endif | |
66 else | |
67 error ("sub2ind: all index arguments must be the same size"); | |
68 endif | |
69 endif | |
70 else | |
71 error ("sub2ind: expecting integer-valued index arguments"); | |
72 endif | |
73 endfor | |
74 else | |
75 error ("sub2ind: expecting dims to be an integer vector"); | |
76 endif | |
77 else | |
6046 | 78 print_usage (); |
4679 | 79 endif |
80 | |
81 | |
82 endfunction | |
6597 | 83 |
84 # Test input validation | |
85 %!error <sub2ind: expecting dims to be an integer vector> sub2ind([10 10.5], 1, 1); | |
86 %!error <sub2ind: expecting integer-valued index arguments> sub2ind([10 10], 1.5, 1); | |
87 %!error <sub2ind: expecting integer-valued index arguments> sub2ind([10 10], 1, 1.5); | |
88 | |
89 # Test evaluation | |
90 %!shared s1, s2, s3, in | |
91 %! s1 = [ 1 1 1 1 ; 2 2 2 2 ]; | |
92 %! s2 = [ 1 1 2 2 ; 1 1 2 2 ]; | |
93 %! s3 = [ 1 2 1 2 ; 1 2 1 2 ]; | |
94 %! in = [ 1 101 11 111 ; 2 102 12 112 ]; | |
95 %!assert (sub2ind([10 10 10], s1, s2, s3), in); | |
96 %!shared | |
97 | |
98 # Test low index | |
99 %!assert (sub2ind([10 10 10], 1, 1, 1), 1); | |
100 %!error <sub2ind: index out of range> sub2ind([10 10 10], 0, 1, 1); | |
101 %!error <sub2ind: index out of range> sub2ind([10 10 10], 1, 0, 1); | |
102 %!error <sub2ind: index out of range> sub2ind([10 10 10], 1, 1, 0); | |
103 | |
104 # Test high index | |
105 %!assert (sub2ind([10 10 10], 10, 10, 10), 1000); | |
106 %!error <sub2ind: index out of range> sub2ind([10 10 10], 11, 10, 10); | |
107 %!error <sub2ind: index out of range> sub2ind([10 10 10], 10, 11, 10); | |
108 %!error <sub2ind: index out of range> sub2ind([10 10 10], 10, 10, 11); | |
109 | |
110 # Test high index in the trailing dimensions | |
111 %!assert (sub2ind([10], 2, 1, 1), 2); | |
112 %!error <sub2ind: index out of range> sub2ind([10], 1, 2, 1); | |
113 %!error <sub2ind: index out of range> sub2ind([10], 1, 1, 2); | |
114 %!assert (sub2ind([10 10], 2, 2, 1), 12); | |
115 %!error <sub2ind: index out of range> sub2ind([10 10], 2, 1, 2); | |
116 %!error <sub2ind: index out of range> sub2ind([10 10], 1, 2, 2); | |
117 | |
118 # Test handling of empty arguments | |
119 %!assert (sub2ind([10 10], zeros(0,0), zeros(0,0)), zeros(0,0)); | |
120 %!assert (sub2ind([10 10], zeros(2,0), zeros(2,0)), zeros(2,0)); | |
121 %!assert (sub2ind([10 10], zeros(0,2), zeros(0,2)), zeros(0,2)); | |
122 %!error <sub2ind: all index arguments must be the same size> sub2ind([10 10 10], zeros(0,2), zeros(2,0)); | |
123 | |
124 # Test handling of arguments of different size | |
125 %!error <sub2ind: all index arguments must be the same size> sub2ind([10 10], ones(1,2), ones(1,3)); | |
126 %!error <sub2ind: all index arguments must be the same size> sub2ind([10 10], ones(1,2), ones(2,1)); | |
127 |