sub2ind.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2009-2012 VZLU Prague
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include "quit.h"
00028 
00029 #include "defun-dld.h"
00030 #include "error.h"
00031 #include "gripes.h"
00032 #include "oct-obj.h"
00033 
00034 
00035 static dim_vector
00036 get_dim_vector (const octave_value& val, const char *name)
00037 {
00038   RowVector dimsv = val.row_vector_value (false, true);
00039   dim_vector dv;
00040   octave_idx_type n = dimsv.length ();
00041 
00042   if (n < 1)
00043     error ("%s: dimension vector DIMS must not be empty", name);
00044   else
00045     {
00046       dv.resize (std::max (n, static_cast<octave_idx_type> (2)));
00047       dv(1) = 1;
00048       for (octave_idx_type i = 0; i < n; i++)
00049         {
00050           octave_idx_type ii = dimsv(i);
00051           if (ii == dimsv(i) && ii >= 0)
00052             dv(i) = ii;
00053           else
00054             {
00055               error ("%s: dimension vector DIMS must contain integers", name);
00056               break;
00057             }
00058         }
00059     }
00060 
00061   return dv;
00062 }
00063 
00064 DEFUN_DLD (sub2ind, args, ,
00065   "-*- texinfo -*-\n\
00066 @deftypefn  {Function File} {@var{ind} =} sub2ind (@var{dims}, @var{i}, @var{j})\n\
00067 @deftypefnx {Function File} {@var{ind} =} sub2ind (@var{dims}, @var{s1}, @var{s2}, @dots{}, @var{sN})\n\
00068 Convert subscripts to a linear index.\n\
00069 \n\
00070 The following example shows how to convert the two-dimensional\n\
00071 index @code{(2,3)} of a 3-by-3 matrix to a linear index.  The matrix\n\
00072 is linearly indexed moving from one column to next, filling up\n\
00073 all rows in each column.\n\
00074 \n\
00075 @example\n\
00076 @group\n\
00077 linear_index = sub2ind ([3, 3], 2, 3)\n\
00078 @result{} 8\n\
00079 @end group\n\
00080 @end example\n\
00081 @seealso{ind2sub}\n\
00082 @end deftypefn")
00083 {
00084   int nargin = args.length ();
00085   octave_value retval;
00086 
00087   if (nargin < 2)
00088     print_usage ();
00089   else
00090     {
00091       dim_vector dv = get_dim_vector (args(0), "sub2ind");
00092       Array<idx_vector> idxa (dim_vector (nargin-1, 1));
00093 
00094       if (! error_state)
00095         {
00096           dv = dv.redim (nargin - 1);
00097           for (int j = 0; j < nargin - 1; j++)
00098             {
00099               if (args(j+1).is_numeric_type ())
00100                 {
00101                   idxa(j) = args(j+1).index_vector ();
00102                   if (error_state)
00103                     break;
00104                   else if (j > 0 && args(j+1).dims () != args(1).dims ())
00105                     error ("sub2ind: all subscripts must be of the same size");
00106                 }
00107               else
00108                 error ("sub2ind: subscripts must be numeric");
00109 
00110               if (error_state)
00111                 break;
00112             }
00113         }
00114 
00115       if (! error_state)
00116         {
00117           idx_vector idx = sub2ind (dv, idxa);
00118           retval = idx;
00119         }
00120     }
00121 
00122   return retval;
00123 }
00124 
00125 /*
00126 
00127 # Test input validation
00128 %!error <sub2ind: dimension vector > sub2ind([10 10.5], 1, 1);
00129 %!error <subscript indices > sub2ind([10 10], 1.5, 1);
00130 %!error <subscript indices > sub2ind([10 10], 1, 1.5);
00131 
00132 # Test evaluation
00133 %!shared s1, s2, s3, in
00134 %! s1 = [   1   1   1   1 ;   2   2   2   2 ];
00135 %! s2 = [   1   1   2   2 ;   1   1   2   2 ];
00136 %! s3 = [   1   2   1   2 ;   1   2   1   2 ];
00137 %! in = [   1 101  11 111 ;   2 102  12 112 ];
00138 %!assert (sub2ind([10 10 10], s1, s2, s3), in);
00139 %!shared
00140 
00141 # Test low index
00142 %!assert (sub2ind([10 10 10], 1, 1, 1), 1);
00143 %!error <subscript indices > sub2ind([10 10 10], 0, 1, 1);
00144 %!error <subscript indices > sub2ind([10 10 10], 1, 0, 1);
00145 %!error <subscript indices > sub2ind([10 10 10], 1, 1, 0);
00146 
00147 # Test high index
00148 %!assert (sub2ind([10 10 10], 10, 10, 10), 1000);
00149 %!error <sub2ind: index out of range> sub2ind([10 10 10], 11, 10, 10);
00150 %!error <sub2ind: index out of range> sub2ind([10 10 10], 10, 11, 10);
00151 %!error <sub2ind: index out of range> sub2ind([10 10 10], 10, 10, 11);
00152 
00153 # Test high index in the trailing dimensions
00154 %!assert (sub2ind([10, 1], 2, 1, 1), 2);
00155 %!error <sub2ind: index out of range> sub2ind([10, 1], 1, 2, 1);
00156 %!error <sub2ind: index out of range> sub2ind([10, 1], 1, 1, 2);
00157 %!assert (sub2ind([10 10], 2, 2, 1), 12);
00158 %!error <sub2ind: index out of range> sub2ind([10 10], 2, 1, 2);
00159 %!error <sub2ind: index out of range> sub2ind([10 10], 1, 2, 2);
00160 
00161 # Test handling of empty arguments
00162 %!assert (sub2ind([10 10], zeros(0,0), zeros(0,0)), zeros(0,0));
00163 %!assert (sub2ind([10 10], zeros(2,0), zeros(2,0)), zeros(2,0));
00164 %!assert (sub2ind([10 10], zeros(0,2), zeros(0,2)), zeros(0,2));
00165 %!error <sub2ind: all subscripts .* same size> sub2ind([10 10 10], zeros(0,2), zeros(2,0));
00166 
00167 # Test handling of arguments of different size
00168 %!error <sub2ind: all subscripts .* same size> sub2ind([10 10], ones(1,2), ones(1,3));
00169 %!error <sub2ind: all subscripts .* same size> sub2ind([10 10], ones(1,2), ones(2,1));
00170 
00171 */
00172 
00173 DEFUN_DLD (ind2sub, args, nargout,
00174   "-*- texinfo -*-\n\
00175 @deftypefn {Function File} {[@var{s1}, @var{s2}, @dots{}, @var{sN}] =} ind2sub (@var{dims}, @var{ind})\n\
00176 Convert a linear index to subscripts.\n\
00177 \n\
00178 The following example shows how to convert the linear index @code{8}\n\
00179 in a 3-by-3 matrix into a subscript.  The matrix is linearly indexed\n\
00180 moving from one column to next, filling up all rows in each column.\n\
00181 \n\
00182 @example\n\
00183 @group\n\
00184 [r, c] = ind2sub ([3, 3], 8)\n\
00185 @result{} r =  2\n\
00186    c =  3\n\
00187 @end group\n\
00188 @end example\n\
00189 @seealso{sub2ind}\n\
00190 @end deftypefn")
00191 {
00192   int nargin = args.length ();
00193   octave_value_list retval;
00194 
00195   if (nargin != 2)
00196     print_usage ();
00197   else
00198     {
00199       dim_vector dv = get_dim_vector (args(0), "ind2sub");
00200       idx_vector idx = args(1).index_vector ();
00201       if (! error_state)
00202         {
00203           if (nargout > dv.length ())
00204             dv = dv.redim (nargout);
00205 
00206           Array<idx_vector> idxa = ind2sub (dv, idx);
00207           retval = Array<octave_value> (idxa);
00208         }
00209     }
00210 
00211   return retval;
00212 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines