annotate scripts/general/sub2ind.m @ 7948:af10baa63915 ss-3-1-50

3.1.50 snapshot
author John W. Eaton <jwe@octave.org>
date Fri, 18 Jul 2008 17:42:48 -0400
parents 74075b3b54c1
children 1bf0ce0930be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7345
74075b3b54c1 [project @ 2008-01-04 18:42:40 by jwe]
jwe
parents: 7017
diff changeset
1 ## Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007, 2008 Paul Kienzle
4679
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
2 ##
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
4 ##
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6631
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6631
diff changeset
8 ## your option) any later version.
4679
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
9 ##
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
14 ##
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6631
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6631
diff changeset
17 ## <http://www.gnu.org/licenses/>.
4679
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
18
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{ind} =} sub2ind (@var{dims}, @var{i}, @var{j})
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefnx {Function File} {@var{ind} =} sub2ind (@var{dims}, @var{s1}, @var{s2}, @dots{}, @var{sN})
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
22 ## Convert subscripts into a linear index.
6631
1b8daf8c0397 [project @ 2007-05-16 16:11:55 by jwe]
jwe
parents: 6597
diff changeset
23 ##
1b8daf8c0397 [project @ 2007-05-16 16:11:55 by jwe]
jwe
parents: 6597
diff changeset
24 ## The following example shows how to convert the two-dimensional
7345
74075b3b54c1 [project @ 2008-01-04 18:42:40 by jwe]
jwe
parents: 7017
diff changeset
25 ## index @code{(2,3)} of a 3-by-3 matrix to a linear index. The matrix
74075b3b54c1 [project @ 2008-01-04 18:42:40 by jwe]
jwe
parents: 7017
diff changeset
26 ## is linearly indexed moving from one column to next, filling up
74075b3b54c1 [project @ 2008-01-04 18:42:40 by jwe]
jwe
parents: 7017
diff changeset
27 ## all rows in each column.
6631
1b8daf8c0397 [project @ 2007-05-16 16:11:55 by jwe]
jwe
parents: 6597
diff changeset
28 ##
1b8daf8c0397 [project @ 2007-05-16 16:11:55 by jwe]
jwe
parents: 6597
diff changeset
29 ## @example
1b8daf8c0397 [project @ 2007-05-16 16:11:55 by jwe]
jwe
parents: 6597
diff changeset
30 ## linear_index = sub2ind ([3, 3], 2, 3)
1b8daf8c0397 [project @ 2007-05-16 16:11:55 by jwe]
jwe
parents: 6597
diff changeset
31 ## @result{} 8
1b8daf8c0397 [project @ 2007-05-16 16:11:55 by jwe]
jwe
parents: 6597
diff changeset
32 ## @end example
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
33 ## @seealso{ind2sub}
4679
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
34 ## @end deftypefn
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
35
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
36 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
37 ## Adapted-by: jwe
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
38
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
39 function ind = sub2ind (dims, varargin)
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
40
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
41 if (nargin > 1)
6050
435294ca4ee6 [project @ 2006-10-13 17:42:21 by jwe]
jwe
parents: 6046
diff changeset
42 if (isvector (dims) && all (round (dims) == dims))
4679
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
43 nd = length (dims);
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
44 vlen = length (varargin);
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
45 dims(vlen) = prod (dims(vlen:nd));
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
46 dims(vlen+1:nd) = [];
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
47 scale = cumprod (dims(:));
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
48 for i = 1:vlen
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
49 arg = varargin{i};
6597
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
50 if (isnumeric (arg) && isequal (round (arg), arg))
4679
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
51 if (i == 1)
6597
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
52 if (all (arg(:) > 0 & arg(:) <= dims(i)))
4679
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
53 ind = first_arg = arg;
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
54 else
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
55 error ("sub2ind: index out of range");
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
56 endif
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
57 else
6597
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
58 if (size_equal (first_arg, arg))
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
59 if ((i > nd && arg == 1) || all (arg(:) > 0 & arg(:) <= dims(i)))
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
60 ind += scale(i-1) * (arg - 1);
4679
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
61 else
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
62 error ("sub2ind: index out of range");
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
63 endif
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
64 else
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
65 error ("sub2ind: all index arguments must be the same size");
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
66 endif
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
67 endif
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
68 else
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
69 error ("sub2ind: expecting integer-valued index arguments");
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
70 endif
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
71 endfor
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
72 else
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
73 error ("sub2ind: expecting dims to be an integer vector");
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
74 endif
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
75 else
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
76 print_usage ();
4679
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
77 endif
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
78
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
79
c0302db81b75 [project @ 2003-12-17 03:31:29 by jwe]
jwe
parents:
diff changeset
80 endfunction
6597
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
81
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
82 # Test input validation
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
83 %!error <sub2ind: expecting dims to be an integer vector> sub2ind([10 10.5], 1, 1);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
84 %!error <sub2ind: expecting integer-valued index arguments> sub2ind([10 10], 1.5, 1);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
85 %!error <sub2ind: expecting integer-valued index arguments> sub2ind([10 10], 1, 1.5);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
86
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
87 # Test evaluation
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
88 %!shared s1, s2, s3, in
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
89 %! s1 = [ 1 1 1 1 ; 2 2 2 2 ];
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
90 %! s2 = [ 1 1 2 2 ; 1 1 2 2 ];
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
91 %! s3 = [ 1 2 1 2 ; 1 2 1 2 ];
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
92 %! in = [ 1 101 11 111 ; 2 102 12 112 ];
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
93 %!assert (sub2ind([10 10 10], s1, s2, s3), in);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
94 %!shared
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
95
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
96 # Test low index
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
97 %!assert (sub2ind([10 10 10], 1, 1, 1), 1);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
98 %!error <sub2ind: index out of range> sub2ind([10 10 10], 0, 1, 1);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
99 %!error <sub2ind: index out of range> sub2ind([10 10 10], 1, 0, 1);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
100 %!error <sub2ind: index out of range> sub2ind([10 10 10], 1, 1, 0);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
101
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
102 # Test high index
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
103 %!assert (sub2ind([10 10 10], 10, 10, 10), 1000);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
104 %!error <sub2ind: index out of range> sub2ind([10 10 10], 11, 10, 10);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
105 %!error <sub2ind: index out of range> sub2ind([10 10 10], 10, 11, 10);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
106 %!error <sub2ind: index out of range> sub2ind([10 10 10], 10, 10, 11);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
107
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
108 # Test high index in the trailing dimensions
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
109 %!assert (sub2ind([10], 2, 1, 1), 2);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
110 %!error <sub2ind: index out of range> sub2ind([10], 1, 2, 1);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
111 %!error <sub2ind: index out of range> sub2ind([10], 1, 1, 2);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
112 %!assert (sub2ind([10 10], 2, 2, 1), 12);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
113 %!error <sub2ind: index out of range> sub2ind([10 10], 2, 1, 2);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
114 %!error <sub2ind: index out of range> sub2ind([10 10], 1, 2, 2);
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
115
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
116 # Test handling of empty arguments
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
117 %!assert (sub2ind([10 10], zeros(0,0), zeros(0,0)), zeros(0,0));
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
118 %!assert (sub2ind([10 10], zeros(2,0), zeros(2,0)), zeros(2,0));
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
119 %!assert (sub2ind([10 10], zeros(0,2), zeros(0,2)), zeros(0,2));
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
120 %!error <sub2ind: all index arguments must be the same size> sub2ind([10 10 10], zeros(0,2), zeros(2,0));
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
121
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
122 # Test handling of arguments of different size
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
123 %!error <sub2ind: all index arguments must be the same size> sub2ind([10 10], ones(1,2), ones(1,3));
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
124 %!error <sub2ind: all index arguments must be the same size> sub2ind([10 10], ones(1,2), ones(2,1));
65919b012b35 [project @ 2007-04-27 16:58:25 by jwe]
jwe
parents: 6050
diff changeset
125