Mercurial > hg > octave-avbm
comparison src/data.cc @ 4567:fc30061d01da
[project @ 2003-10-29 20:11:15 by jwe]
author | jwe |
---|---|
date | Wed, 29 Oct 2003 20:11:16 +0000 |
parents | 1db951a4fcd5 |
children | 01e4957409a4 |
comparison
equal
deleted
inserted
replaced
4566:30ba814d6700 | 4567:fc30061d01da |
---|---|
1234 } | 1234 } |
1235 | 1235 |
1236 return retval; | 1236 return retval; |
1237 } | 1237 } |
1238 | 1238 |
1239 DEFUN (reshape, args, , | |
1240 "-*- texinfo -*-\n\ | |
1241 @deftypefn {Function File} {} reshape (@var{a}, @var{m}, @var{n}, @dots{})\n\ | |
1242 @deftypefnx {Function File} {} reshape (@var{a}, @var{siz})\n\ | |
1243 Return a matrix with the given dimensions whose elements are taken\n\ | |
1244 from the matrix @var{a}. The elements of the matrix are access in\n\ | |
1245 column-major order (like Fortran arrays are stored).\n\ | |
1246 \n\ | |
1247 For example,\n\ | |
1248 \n\ | |
1249 @example\n\ | |
1250 @group\n\ | |
1251 reshape ([1, 2, 3, 4], 2, 2)\n\ | |
1252 @result{} 1 3\n\ | |
1253 2 4\n\ | |
1254 @end group\n\ | |
1255 @end example\n\ | |
1256 \n\ | |
1257 @noindent\n\ | |
1258 Note that the total number of elements in the original\n\ | |
1259 matrix must match the total number of elements in the new matrix.\n\ | |
1260 @end deftypefn") | |
1261 { | |
1262 octave_value retval; | |
1263 | |
1264 int nargin = args.length (); | |
1265 | |
1266 Array<int> new_size; | |
1267 | |
1268 if (nargin == 2) | |
1269 new_size = args(1).int_vector_value (); | |
1270 else if (nargin > 2) | |
1271 { | |
1272 new_size.resize (nargin-1); | |
1273 | |
1274 for (int i = 1; i < nargin; i++) | |
1275 { | |
1276 new_size(i-1) = args(i).int_value (); | |
1277 | |
1278 if (error_state) | |
1279 break; | |
1280 } | |
1281 } | |
1282 else | |
1283 { | |
1284 print_usage ("reshape"); | |
1285 return retval; | |
1286 } | |
1287 | |
1288 if (error_state) | |
1289 { | |
1290 error ("reshape: invalid arguments"); | |
1291 return retval; | |
1292 } | |
1293 | |
1294 int n = new_size.length (); | |
1295 | |
1296 if (n < 2) | |
1297 { | |
1298 error ("reshape: expecting size to be vector with at least 2 elements"); | |
1299 return retval; | |
1300 } | |
1301 | |
1302 dim_vector new_dims; | |
1303 | |
1304 new_dims.resize (n); | |
1305 | |
1306 for (int i = 0; i < n; i++) | |
1307 new_dims(i) = new_size(i); | |
1308 | |
1309 octave_value arg = args(0); | |
1310 | |
1311 if (new_dims.numel () == arg.numel ()) | |
1312 retval = (new_dims == arg.dims ()) ? arg : arg.reshape (new_dims); | |
1313 else | |
1314 error ("reshape: size mismatch"); | |
1315 | |
1316 return retval; | |
1317 } | |
1318 | |
1239 DEFUN (squeeze, args, , | 1319 DEFUN (squeeze, args, , |
1240 "-*- texinfo -*-\n\ | 1320 "-*- texinfo -*-\n\ |
1241 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ | 1321 @deftypefn {Built-in Function} {} squeeze (@var{x})\n\ |
1242 Remove singleton dimensions from @var{x} and return the result.\n\ | 1322 Remove singleton dimensions from @var{x} and return the result.\n\ |
1243 @end deftypefn") | 1323 @end deftypefn") |