Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
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 }