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