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 |
|
30 ## linear_index = sub2ind ([3, 3], 2, 3) |
|
31 ## @result{} 8 |
|
32 ## @end example |
5642
|
33 ## @seealso{ind2sub} |
4679
|
34 ## @end deftypefn |
|
35 |
|
36 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
37 ## Adapted-by: jwe |
|
38 |
|
39 function ind = sub2ind (dims, varargin) |
|
40 |
|
41 if (nargin > 1) |
6050
|
42 if (isvector (dims) && all (round (dims) == dims)) |
4679
|
43 nd = length (dims); |
|
44 vlen = length (varargin); |
|
45 dims(vlen) = prod (dims(vlen:nd)); |
|
46 dims(vlen+1:nd) = []; |
|
47 scale = cumprod (dims(:)); |
|
48 for i = 1:vlen |
|
49 arg = varargin{i}; |
6597
|
50 if (isnumeric (arg) && isequal (round (arg), arg)) |
4679
|
51 if (i == 1) |
6597
|
52 if (all (arg(:) > 0 & arg(:) <= dims(i))) |
4679
|
53 ind = first_arg = arg; |
|
54 else |
|
55 error ("sub2ind: index out of range"); |
|
56 endif |
|
57 else |
6597
|
58 if (size_equal (first_arg, arg)) |
|
59 if ((i > nd && arg == 1) || all (arg(:) > 0 & arg(:) <= dims(i))) |
|
60 ind += scale(i-1) * (arg - 1); |
4679
|
61 else |
|
62 error ("sub2ind: index out of range"); |
|
63 endif |
|
64 else |
|
65 error ("sub2ind: all index arguments must be the same size"); |
|
66 endif |
|
67 endif |
|
68 else |
|
69 error ("sub2ind: expecting integer-valued index arguments"); |
|
70 endif |
|
71 endfor |
|
72 else |
|
73 error ("sub2ind: expecting dims to be an integer vector"); |
|
74 endif |
|
75 else |
6046
|
76 print_usage (); |
4679
|
77 endif |
|
78 |
|
79 |
|
80 endfunction |
6597
|
81 |
|
82 # Test input validation |
|
83 %!error <sub2ind: expecting dims to be an integer vector> sub2ind([10 10.5], 1, 1); |
|
84 %!error <sub2ind: expecting integer-valued index arguments> sub2ind([10 10], 1.5, 1); |
|
85 %!error <sub2ind: expecting integer-valued index arguments> sub2ind([10 10], 1, 1.5); |
|
86 |
|
87 # Test evaluation |
|
88 %!shared s1, s2, s3, in |
|
89 %! s1 = [ 1 1 1 1 ; 2 2 2 2 ]; |
|
90 %! s2 = [ 1 1 2 2 ; 1 1 2 2 ]; |
|
91 %! s3 = [ 1 2 1 2 ; 1 2 1 2 ]; |
|
92 %! in = [ 1 101 11 111 ; 2 102 12 112 ]; |
|
93 %!assert (sub2ind([10 10 10], s1, s2, s3), in); |
|
94 %!shared |
|
95 |
|
96 # Test low index |
|
97 %!assert (sub2ind([10 10 10], 1, 1, 1), 1); |
|
98 %!error <sub2ind: index out of range> sub2ind([10 10 10], 0, 1, 1); |
|
99 %!error <sub2ind: index out of range> sub2ind([10 10 10], 1, 0, 1); |
|
100 %!error <sub2ind: index out of range> sub2ind([10 10 10], 1, 1, 0); |
|
101 |
|
102 # Test high index |
|
103 %!assert (sub2ind([10 10 10], 10, 10, 10), 1000); |
|
104 %!error <sub2ind: index out of range> sub2ind([10 10 10], 11, 10, 10); |
|
105 %!error <sub2ind: index out of range> sub2ind([10 10 10], 10, 11, 10); |
|
106 %!error <sub2ind: index out of range> sub2ind([10 10 10], 10, 10, 11); |
|
107 |
|
108 # Test high index in the trailing dimensions |
|
109 %!assert (sub2ind([10], 2, 1, 1), 2); |
|
110 %!error <sub2ind: index out of range> sub2ind([10], 1, 2, 1); |
|
111 %!error <sub2ind: index out of range> sub2ind([10], 1, 1, 2); |
|
112 %!assert (sub2ind([10 10], 2, 2, 1), 12); |
|
113 %!error <sub2ind: index out of range> sub2ind([10 10], 2, 1, 2); |
|
114 %!error <sub2ind: index out of range> sub2ind([10 10], 1, 2, 2); |
|
115 |
|
116 # Test handling of empty arguments |
|
117 %!assert (sub2ind([10 10], zeros(0,0), zeros(0,0)), zeros(0,0)); |
|
118 %!assert (sub2ind([10 10], zeros(2,0), zeros(2,0)), zeros(2,0)); |
|
119 %!assert (sub2ind([10 10], zeros(0,2), zeros(0,2)), zeros(0,2)); |
|
120 %!error <sub2ind: all index arguments must be the same size> sub2ind([10 10 10], zeros(0,2), zeros(2,0)); |
|
121 |
|
122 # Test handling of arguments of different size |
|
123 %!error <sub2ind: all index arguments must be the same size> sub2ind([10 10], ones(1,2), ones(1,3)); |
|
124 %!error <sub2ind: all index arguments must be the same size> sub2ind([10 10], ones(1,2), ones(2,1)); |
|
125 |