Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/cellfun.cc @ 8994:a8d30dc1beec
cellfun optimizations
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 18 Mar 2009 12:06:46 +0100 |
parents | 193804a4f82f |
children | 97aa01a85ea4 |
rev | line source |
---|---|
5205 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2005, 2006, 2007, 2008, 2009 Mohamed Kamoun |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
4 Copyright (C) 2009 Jaroslav Hajek |
5205 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
5205 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
5205 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <string> | |
5988 | 29 #include <vector> |
30 #include <list> | |
8994 | 31 #include <memory> |
5205 | 32 |
33 #include "lo-mappers.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8373
diff
changeset
|
34 #include "oct-locbuf.h" |
5205 | 35 |
36 #include "Cell.h" | |
5988 | 37 #include "oct-map.h" |
5205 | 38 #include "defun-dld.h" |
39 #include "parse.h" | |
40 #include "variables.h" | |
5451 | 41 #include "ov-colon.h" |
5988 | 42 #include "unwind-prot.h" |
5205 | 43 |
8994 | 44 // Rationale: |
45 // The octave_base_value::subsasgn method carries too much overhead for | |
46 // per-element assignment strategy. | |
47 // This class will optimize the most optimistic and most likely case | |
48 // when the output really is scalar by defining a hierarchy of virtual | |
49 // collectors specialized for some scalar types. | |
50 | |
51 class scalar_col_helper | |
52 { | |
53 public: | |
54 virtual bool collect (octave_idx_type i, const octave_value& val) = 0; | |
55 virtual octave_value result (void) = 0; | |
56 virtual ~scalar_col_helper (void) { } | |
57 }; | |
58 | |
59 // The default collector represents what was previously done in the main loop. | |
60 // This reuses the existing assignment machinery via octave_value::subsasgn, | |
61 // which can perform all sorts of conversions, but is relatively slow. | |
62 | |
63 class scalar_col_helper_def : public scalar_col_helper | |
64 { | |
65 std::list<octave_value_list> idx_list; | |
66 octave_value resval; | |
67 public: | |
68 scalar_col_helper_def (const octave_value& val, const dim_vector& dims) | |
69 : idx_list (1), resval (val) | |
70 { | |
71 idx_list.front ().resize (1); | |
72 if (resval.dims () != dims) | |
73 resval.resize (dims); | |
74 } | |
75 ~scalar_col_helper_def (void) { } | |
76 | |
77 bool collect (octave_idx_type i, const octave_value& val) | |
78 { | |
79 if (val.numel () == 1) | |
80 { | |
81 idx_list.front ()(0) = static_cast<double> (i + 1); | |
82 resval = resval.subsasgn ("(", idx_list, val); | |
83 } | |
84 else | |
85 error ("cellfun: expecting all values to be scalars for UniformOutput = true"); | |
86 | |
87 return true; | |
88 } | |
89 octave_value result (void) | |
90 { | |
91 return resval; | |
92 } | |
93 }; | |
94 | |
95 template <class T> | |
96 struct scalar_query_helper { }; | |
97 | |
98 #define DEF_QUERY_HELPER(T, TEST, QUERY) \ | |
99 template <> \ | |
100 struct scalar_query_helper<T> \ | |
101 { \ | |
102 static bool has_value (const octave_value& val) \ | |
103 { return TEST; } \ | |
104 static T get_value (const octave_value& val) \ | |
105 { return QUERY; } \ | |
106 } | |
107 | |
108 DEF_QUERY_HELPER (double, val.is_real_scalar (), val.scalar_value ()); | |
109 DEF_QUERY_HELPER (Complex, val.is_complex_scalar (), val.complex_value ()); | |
110 DEF_QUERY_HELPER (float, val.is_single_type () && val.is_real_scalar (), | |
111 val.float_scalar_value ()); | |
112 DEF_QUERY_HELPER (FloatComplex, val.is_single_type () && val.is_complex_scalar (), | |
113 val.float_complex_value ()); | |
114 DEF_QUERY_HELPER (bool, val.is_bool_scalar (), val.bool_value ()); | |
115 // FIXME: More? | |
116 | |
117 // This specializes for collecting elements of a single type, by accessing | |
118 // an array directly. If the scalar is not valid, it returns false. | |
119 | |
120 template <class NDA> | |
121 class scalar_col_helper_nda : public scalar_col_helper | |
122 { | |
123 NDA arrayval; | |
124 typedef typename NDA::element_type T; | |
125 public: | |
126 scalar_col_helper_nda (const octave_value& val, const dim_vector& dims) | |
127 : arrayval (dims) | |
128 { | |
129 arrayval(0) = scalar_query_helper<T>::get_value (val); | |
130 } | |
131 ~scalar_col_helper_nda (void) { } | |
132 | |
133 bool collect (octave_idx_type i, const octave_value& val) | |
134 { | |
135 bool retval = scalar_query_helper<T>::has_value (val); | |
136 if (retval) | |
137 arrayval(i) = scalar_query_helper<T>::get_value (val); | |
138 return retval; | |
139 } | |
140 octave_value result (void) | |
141 { | |
142 return arrayval; | |
143 } | |
144 }; | |
145 | |
146 template class scalar_col_helper_nda<NDArray>; | |
147 template class scalar_col_helper_nda<FloatNDArray>; | |
148 template class scalar_col_helper_nda<ComplexNDArray>; | |
149 template class scalar_col_helper_nda<FloatComplexNDArray>; | |
150 template class scalar_col_helper_nda<boolNDArray>; | |
151 | |
152 // the virtual constructor. | |
153 scalar_col_helper * | |
154 make_col_helper (const octave_value& val, const dim_vector& dims) | |
155 { | |
156 scalar_col_helper *retval; | |
157 | |
158 if (val.is_bool_scalar ()) | |
159 retval = new scalar_col_helper_nda<boolNDArray> (val, dims); | |
160 else if (val.is_complex_scalar ()) | |
161 { | |
162 if (val.is_single_type ()) | |
163 retval = new scalar_col_helper_nda<FloatComplexNDArray> (val, dims); | |
164 else | |
165 retval = new scalar_col_helper_nda<ComplexNDArray> (val, dims); | |
166 } | |
167 else if (val.is_real_scalar ()) | |
168 { | |
169 if (val.is_single_type ()) | |
170 retval = new scalar_col_helper_nda<FloatNDArray> (val, dims); | |
171 else | |
172 retval = new scalar_col_helper_nda<NDArray> (val, dims); | |
173 } | |
174 else | |
175 retval = new scalar_col_helper_def (val, dims); | |
176 | |
177 return retval; | |
178 } | |
179 | |
5988 | 180 DEFUN_DLD (cellfun, args, nargout, |
8715 | 181 "-*- texinfo -*-\n\ |
6854 | 182 @deftypefn {Loadable Function} {} cellfun (@var{name}, @var{c})\n\ |
183 @deftypefnx {Loadable Function} {} cellfun (\"size\", @var{c}, @var{k})\n\ | |
184 @deftypefnx {Loadable Function} {} cellfun (\"isclass\", @var{c}, @var{class})\n\ | |
185 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{c})\n\ | |
186 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{c}, @var{d})\n\ | |
187 @deftypefnx {Loadable Function} {[@var{a}, @var{b}] =} cellfun (@dots{})\n\ | |
188 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'ErrorHandler', @var{errfunc})\n\ | |
189 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'UniformOutput', @var{val})\n\ | |
5205 | 190 \n\ |
191 Evaluate the function named @var{name} on the elements of the cell array\n\ | |
5543 | 192 @var{c}. Elements in @var{c} are passed on to the named function\n\ |
5205 | 193 individually. The function @var{name} can be one of the functions\n\ |
194 \n\ | |
195 @table @code\n\ | |
196 @item isempty\n\ | |
5543 | 197 Return 1 for empty elements.\n\ |
5205 | 198 @item islogical\n\ |
199 Return 1 for logical elements.\n\ | |
200 @item isreal\n\ | |
201 Return 1 for real elements.\n\ | |
202 @item length\n\ | |
203 Return a vector of the lengths of cell elements.\n\ | |
5543 | 204 @item ndims\n\ |
5205 | 205 Return the number of dimensions of each element.\n\ |
206 @item prodofsize\n\ | |
207 Return the product of dimensions of each element.\n\ | |
208 @item size\n\ | |
209 Return the size along the @var{k}-th dimension.\n\ | |
210 @item isclass\n\ | |
211 Return 1 for elements of @var{class}.\n\ | |
212 @end table\n\ | |
213 \n\ | |
214 Additionally, @code{cellfun} accepts an arbitrary function @var{func}\n\ | |
215 in the form of an inline function, function handle, or the name of a\n\ | |
5988 | 216 function (in a character string). In the case of a character string\n\ |
217 argument, the function must accept a single argument named @var{x}, and\n\ | |
218 it must return a string value. The function can take one or more arguments,\n\ | |
219 with the inputs args given by @var{c}, @var{d}, etc. Equally the function\n\ | |
220 can return one or more output arguments. For example\n\ | |
5205 | 221 \n\ |
222 @example\n\ | |
223 @group\n\ | |
5988 | 224 cellfun (@@atan2, @{1, 0@}, @{0, 1@})\n\ |
225 @result{}ans = [1.57080 0.00000]\n\ | |
226 @end group\n\ | |
227 @end example\n\ | |
228 \n\ | |
229 Note that the default output argument is an array of the same size as the\n\ | |
230 input arguments.\n\ | |
231 \n\ | |
8828 | 232 If the parameter 'UniformOutput' is set to true (the default), then the function\n\ |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8286
diff
changeset
|
233 must return a single element which will be concatenated into the\n\ |
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8286
diff
changeset
|
234 return value. If 'UniformOutput' is false, the outputs are concatenated in\n\ |
5988 | 235 a cell array. For example\n\ |
236 \n\ | |
237 @example\n\ | |
238 @group\n\ | |
6623 | 239 cellfun (\"tolower(x)\", @{\"Foo\", \"Bar\", \"FooBar\"@},\n\ |
240 \"UniformOutput\",false)\n\ | |
5205 | 241 @result{} ans = @{\"foo\", \"bar\", \"foobar\"@}\n\ |
242 @end group\n\ | |
243 @end example\n\ | |
5988 | 244 \n\ |
245 Given the parameter 'ErrorHandler', then @var{errfunc} defines a function to\n\ | |
246 call in case @var{func} generates an error. The form of the function is\n\ | |
247 \n\ | |
248 @example\n\ | |
249 function [@dots{}] = errfunc (@var{s}, @dots{})\n\ | |
250 @end example\n\ | |
251 \n\ | |
252 where there is an additional input argument to @var{errfunc} relative to\n\ | |
253 @var{func}, given by @var{s}. This is a structure with the elements\n\ | |
254 'identifier', 'message' and 'index', giving respectively the error\n\ | |
255 identifier, the error message, and the index into the input arguments\n\ | |
256 of the element that caused the error. For example\n\ | |
257 \n\ | |
258 @example\n\ | |
259 @group\n\ | |
260 function y = foo (s, x), y = NaN; endfunction\n\ | |
261 cellfun (@@factorial, @{-1,2@},'ErrorHandler',@@foo)\n\ | |
262 @result{} ans = [NaN 2]\n\ | |
263 @end group\n\ | |
264 @end example\n\ | |
265 \n\ | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8096
diff
changeset
|
266 @seealso{isempty, islogical, isreal, length, ndims, numel, size}\n\ |
5642 | 267 @end deftypefn") |
5205 | 268 { |
5988 | 269 octave_value_list retval; |
5205 | 270 std::string name = "function"; |
271 octave_function *func = 0; | |
272 int nargin = args.length (); | |
5988 | 273 nargout = (nargout < 1 ? 1 : nargout); |
5205 | 274 |
275 if (nargin < 2) | |
276 { | |
277 error ("cellfun: you must supply at least 2 arguments"); | |
5823 | 278 print_usage (); |
5205 | 279 return retval; |
280 } | |
281 | |
282 if (args(0).is_function_handle () || args(0).is_inline_function ()) | |
283 { | |
284 func = args(0).function_value (); | |
285 | |
286 if (error_state) | |
287 return retval; | |
288 } | |
289 else if (args(0).is_string ()) | |
290 name = args(0).string_value (); | |
291 else | |
292 { | |
5543 | 293 error ("cellfun: first argument must be a string or function handle"); |
5205 | 294 return retval; |
295 } | |
296 | |
297 if (! args(1).is_cell ()) | |
298 { | |
5543 | 299 error ("cellfun: second argument must be a cell array"); |
5205 | 300 |
301 return retval; | |
302 } | |
303 | |
8994 | 304 const Cell f_args = args(1).cell_value (); |
5205 | 305 |
5988 | 306 octave_idx_type k = f_args.numel (); |
5205 | 307 |
308 if (name == "isempty") | |
309 { | |
310 boolNDArray result (f_args.dims ()); | |
5988 | 311 for (octave_idx_type count = 0; count < k ; count++) |
5543 | 312 result(count) = f_args.elem(count).is_empty (); |
5988 | 313 retval(0) = result; |
5205 | 314 } |
315 else if (name == "islogical") | |
316 { | |
317 boolNDArray result (f_args.dims ()); | |
5988 | 318 for (octave_idx_type count= 0; count < k ; count++) |
5205 | 319 result(count) = f_args.elem(count).is_bool_type (); |
5988 | 320 retval(0) = result; |
5205 | 321 } |
322 else if (name == "isreal") | |
323 { | |
324 boolNDArray result (f_args.dims ()); | |
5988 | 325 for (octave_idx_type count= 0; count < k ; count++) |
5205 | 326 result(count) = f_args.elem(count).is_real_type (); |
5988 | 327 retval(0) = result; |
5205 | 328 } |
329 else if (name == "length") | |
330 { | |
331 NDArray result (f_args.dims ()); | |
5988 | 332 for (octave_idx_type count= 0; count < k ; count++) |
5544 | 333 result(count) = static_cast<double> (f_args.elem(count).length ()); |
5988 | 334 retval(0) = result; |
5205 | 335 } |
336 else if (name == "ndims") | |
337 { | |
338 NDArray result (f_args.dims ()); | |
5988 | 339 for (octave_idx_type count = 0; count < k ; count++) |
5544 | 340 result(count) = static_cast<double> (f_args.elem(count).ndims ()); |
5988 | 341 retval(0) = result; |
5205 | 342 } |
8994 | 343 else if (name == "prodofsize" || name == "numel") |
5205 | 344 { |
345 NDArray result (f_args.dims ()); | |
5988 | 346 for (octave_idx_type count = 0; count < k ; count++) |
5544 | 347 result(count) = static_cast<double> (f_args.elem(count).numel ()); |
5988 | 348 retval(0) = result; |
5205 | 349 } |
350 else if (name == "size") | |
351 { | |
352 if (nargin == 3) | |
353 { | |
354 int d = args(2).nint_value () - 1; | |
355 | |
356 if (d < 0) | |
7001 | 357 error ("cellfun: third argument must be a positive integer"); |
5205 | 358 |
6484 | 359 if (! error_state) |
5205 | 360 { |
361 NDArray result (f_args.dims ()); | |
5988 | 362 for (octave_idx_type count = 0; count < k ; count++) |
5205 | 363 { |
364 dim_vector dv = f_args.elem(count).dims (); | |
365 if (d < dv.length ()) | |
5544 | 366 result(count) = static_cast<double> (dv(d)); |
5205 | 367 else |
368 result(count) = 1.0; | |
369 } | |
5988 | 370 retval(0) = result; |
5205 | 371 } |
372 } | |
373 else | |
5543 | 374 error ("not enough arguments for `size'"); |
5205 | 375 } |
376 else if (name == "isclass") | |
377 { | |
378 if (nargin == 3) | |
379 { | |
380 std::string class_name = args(2).string_value(); | |
381 boolNDArray result (f_args.dims ()); | |
5988 | 382 for (octave_idx_type count = 0; count < k ; count++) |
5205 | 383 result(count) = (f_args.elem(count).class_name() == class_name); |
384 | |
5988 | 385 retval(0) = result; |
5205 | 386 } |
387 else | |
5543 | 388 error ("not enough arguments for `isclass'"); |
5205 | 389 } |
390 else | |
391 { | |
5988 | 392 unwind_protect::begin_frame ("Fcellfun"); |
393 unwind_protect_int (buffer_error_messages); | |
394 | |
5205 | 395 std::string fcn_name; |
396 | |
397 if (! func) | |
398 { | |
399 fcn_name = unique_symbol_name ("__cellfun_fcn_"); | |
400 std::string fname = "function y = "; | |
401 fname.append (fcn_name); | |
402 fname.append ("(x) y = "); | |
403 func = extract_function (args(0), "cellfun", fcn_name, fname, | |
404 "; endfunction"); | |
405 } | |
406 | |
407 if (! func) | |
408 error ("unknown function"); | |
409 else | |
410 { | |
5988 | 411 octave_value_list inputlist; |
6484 | 412 bool uniform_output = true; |
413 bool have_error_handler = false; | |
5988 | 414 std::string err_name; |
6484 | 415 octave_function *error_handler = 0; |
5988 | 416 int offset = 1; |
417 int i = 1; | |
418 OCTAVE_LOCAL_BUFFER (Cell, inputs, nargin); | |
8994 | 419 // This is to prevent copy-on-write. |
420 const Cell *cinputs = inputs; | |
5205 | 421 |
5988 | 422 while (i < nargin) |
5205 | 423 { |
5988 | 424 if (args(i).is_string()) |
425 { | |
426 std::string arg = args(i++).string_value(); | |
427 if (i == nargin) | |
428 { | |
429 error ("cellfun: parameter value is missing"); | |
430 goto cellfun_err; | |
431 } | |
432 | |
433 std::transform (arg.begin (), arg.end (), | |
434 arg.begin (), tolower); | |
435 | |
436 if (arg == "uniformoutput") | |
6484 | 437 uniform_output = args(i++).bool_value(); |
5988 | 438 else if (arg == "errorhandler") |
439 { | |
440 if (args(i).is_function_handle () || | |
441 args(i).is_inline_function ()) | |
442 { | |
6484 | 443 error_handler = args(i).function_value (); |
5205 | 444 |
5988 | 445 if (error_state) |
446 goto cellfun_err; | |
447 } | |
448 else if (args(i).is_string ()) | |
449 { | |
450 err_name = unique_symbol_name ("__cellfun_fcn_"); | |
451 std::string fname = "function y = "; | |
452 fname.append (fcn_name); | |
453 fname.append ("(x) y = "); | |
6484 | 454 error_handler = extract_function (args(i), "cellfun", |
455 err_name, fname, | |
456 "; endfunction"); | |
5988 | 457 } |
458 | |
6484 | 459 if (! error_handler) |
5988 | 460 goto cellfun_err; |
461 | |
6484 | 462 have_error_handler = true; |
5988 | 463 i++; |
464 } | |
465 else | |
466 { | |
467 error ("cellfun: unrecognized parameter %s", | |
468 arg.c_str()); | |
469 goto cellfun_err; | |
470 } | |
471 offset += 2; | |
472 } | |
473 else | |
474 { | |
475 inputs[i-offset] = args(i).cell_value (); | |
476 if (f_args.dims() != inputs[i-offset].dims()) | |
477 { | |
478 error ("cellfun: Dimension mismatch"); | |
479 goto cellfun_err; | |
480 | |
481 } | |
482 i++; | |
483 } | |
5205 | 484 } |
485 | |
8994 | 486 nargin -= offset; |
487 inputlist.resize(nargin); | |
5988 | 488 |
6484 | 489 if (have_error_handler) |
5988 | 490 buffer_error_messages++; |
491 | |
6484 | 492 if (uniform_output) |
5988 | 493 { |
8994 | 494 OCTAVE_LOCAL_BUFFER (std::auto_ptr<scalar_col_helper>, retptr, nargout); |
5988 | 495 |
496 for (octave_idx_type count = 0; count < k ; count++) | |
497 { | |
8994 | 498 for (int j = 0; j < nargin; j++) |
499 inputlist(j) = cinputs[j](count); | |
5988 | 500 |
501 octave_value_list tmp = feval (func, inputlist, nargout); | |
502 | |
6484 | 503 if (error_state && have_error_handler) |
5988 | 504 { |
505 Octave_map msg; | |
7977
065c28e1c368
Modify Fcellfun to directly access the error message/id rather than use a call to Flasterr
David Bateman <dbateman@free.fr>
parents:
7038
diff
changeset
|
506 msg.assign ("identifier", last_error_id ()); |
065c28e1c368
Modify Fcellfun to directly access the error message/id rather than use a call to Flasterr
David Bateman <dbateman@free.fr>
parents:
7038
diff
changeset
|
507 msg.assign ("message", last_error_message ()); |
065c28e1c368
Modify Fcellfun to directly access the error message/id rather than use a call to Flasterr
David Bateman <dbateman@free.fr>
parents:
7038
diff
changeset
|
508 msg.assign ("index", octave_value(double (count + static_cast<octave_idx_type>(1)))); |
5988 | 509 octave_value_list errlist = inputlist; |
510 errlist.prepend (msg); | |
511 buffer_error_messages--; | |
512 error_state = 0; | |
6484 | 513 tmp = feval (error_handler, errlist, nargout); |
5988 | 514 buffer_error_messages++; |
515 | |
516 if (error_state) | |
517 goto cellfun_err; | |
518 } | |
519 | |
520 if (tmp.length() < nargout) | |
521 { | |
522 error ("cellfun: too many output arguments"); | |
523 goto cellfun_err; | |
524 } | |
525 | |
526 if (error_state) | |
527 break; | |
528 | |
529 if (count == 0) | |
530 { | |
5989 | 531 for (int j = 0; j < nargout; j++) |
5988 | 532 { |
8994 | 533 octave_value val = tmp(j); |
5988 | 534 |
8994 | 535 if (val.numel () == 1) |
536 retptr[j].reset (make_col_helper (val, f_args.dims ())); | |
537 else | |
538 { | |
539 error ("cellfun: expecting all values to be scalars for UniformOutput = true"); | |
540 break; | |
541 } | |
5988 | 542 } |
543 } | |
544 else | |
545 { | |
5989 | 546 for (int j = 0; j < nargout; j++) |
547 { | |
548 octave_value val = tmp(j); | |
549 | |
8994 | 550 if (! retptr[j]->collect (count, val)) |
551 { | |
552 // FIXME: A more elaborate structure would allow again a virtual | |
553 // constructor here. | |
554 retptr[j].reset (new scalar_col_helper_def (retptr[j]->result (), | |
555 f_args.dims ())); | |
556 retptr[j]->collect (count, val); | |
557 } | |
558 } | |
5988 | 559 } |
560 | |
561 if (error_state) | |
562 break; | |
563 } | |
8994 | 564 |
565 retval.resize (nargout); | |
566 for (int j = 0; j < nargout; j++) | |
567 { | |
568 if (retptr[j].get ()) | |
569 retval(j) = retptr[j]->result (); | |
570 else | |
571 retval(j) = Matrix (); | |
572 } | |
5988 | 573 } |
574 else | |
575 { | |
576 OCTAVE_LOCAL_BUFFER (Cell, results, nargout); | |
5989 | 577 for (int j = 0; j < nargout; j++) |
578 results[j].resize(f_args.dims()); | |
5988 | 579 |
580 for (octave_idx_type count = 0; count < k ; count++) | |
581 { | |
8994 | 582 for (int j = 0; j < nargin; j++) |
583 inputlist(j) = cinputs[j](count); | |
5988 | 584 |
585 octave_value_list tmp = feval (func, inputlist, nargout); | |
586 | |
6484 | 587 if (error_state && have_error_handler) |
5988 | 588 { |
589 Octave_map msg; | |
7977
065c28e1c368
Modify Fcellfun to directly access the error message/id rather than use a call to Flasterr
David Bateman <dbateman@free.fr>
parents:
7038
diff
changeset
|
590 msg.assign ("identifier", last_error_id ()); |
065c28e1c368
Modify Fcellfun to directly access the error message/id rather than use a call to Flasterr
David Bateman <dbateman@free.fr>
parents:
7038
diff
changeset
|
591 msg.assign ("message", last_error_message ()); |
065c28e1c368
Modify Fcellfun to directly access the error message/id rather than use a call to Flasterr
David Bateman <dbateman@free.fr>
parents:
7038
diff
changeset
|
592 msg.assign ("index", octave_value(double (count + static_cast<octave_idx_type>(1)))); |
5988 | 593 octave_value_list errlist = inputlist; |
594 errlist.prepend (msg); | |
595 buffer_error_messages--; | |
596 error_state = 0; | |
6484 | 597 tmp = feval (error_handler, errlist, nargout); |
5988 | 598 buffer_error_messages++; |
599 | |
600 if (error_state) | |
601 goto cellfun_err; | |
602 } | |
603 | |
604 if (tmp.length() < nargout) | |
605 { | |
606 error ("cellfun: too many output arguments"); | |
607 goto cellfun_err; | |
608 } | |
609 | |
610 if (error_state) | |
611 break; | |
612 | |
613 | |
5989 | 614 for (int j = 0; j < nargout; j++) |
615 results[j](count) = tmp(j); | |
5988 | 616 } |
617 | |
618 retval.resize(nargout); | |
5989 | 619 for (int j = 0; j < nargout; j++) |
620 retval(j) = results[j]; | |
5988 | 621 } |
622 | |
623 cellfun_err: | |
624 if (error_state) | |
625 retval = octave_value_list(); | |
5205 | 626 |
627 if (! fcn_name.empty ()) | |
628 clear_function (fcn_name); | |
5988 | 629 |
630 if (! err_name.empty ()) | |
631 clear_function (err_name); | |
5205 | 632 } |
5988 | 633 |
634 unwind_protect::run_frame ("Fcellfun"); | |
5205 | 635 } |
636 | |
637 return retval; | |
638 } | |
639 | |
5988 | 640 /* |
641 | |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
642 %% Test function to check the "Errorhandler" option |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
643 %!function [z] = cellfunerror (S, varargin) |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
644 %! z = S; |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
645 %! endfunction |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
646 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
647 %% First input argument can be a string, an inline function, |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
648 %% a function_handle or an anonymous function |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
649 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
650 %! A = cellfun ("islogical", {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
651 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
652 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
653 %! A = cellfun (inline ("islogical (x)", "x"), {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
654 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
655 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
656 %! A = cellfun (@islogical, {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
657 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
658 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
659 %! A = cellfun (@(x) islogical(x), {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
660 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
661 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
662 %% First input argument can be the special string "isreal", |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
663 %% "isempty", "islogical", "length", "ndims" or "prodofsize" |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
664 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
665 %! A = cellfun ("isreal", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
666 %! assert (A, [true, true, true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
667 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
668 %! A = cellfun ("isempty", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
669 %! assert (A, [false, false, false, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
670 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
671 %! A = cellfun ("islogical", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
672 %! assert (A, [true, false, true, false, false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
673 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
674 %! A = cellfun ("length", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
675 %! assert (A, [1, 1, 1, 1, 0, 3]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
676 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
677 %! A = cellfun ("ndims", {[1, 2; 3, 4]; (cell (1,2,3,4))}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
678 %! assert (A, [2; 4]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
679 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
680 %! A = cellfun ("prodofsize", {[1, 2; 3, 4], (cell (1,2,3,4))}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
681 %! assert (A, [4, 24]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
682 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
683 %% Number of input and output arguments may not be limited to one |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
684 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
685 %! A = cellfun (@(x,y,z) x + y + z, {1, 1, 1}, {2, 2, 2}, {3, 4, 5}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
686 %! assert (A, [6, 7, 8]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
687 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
688 %! A = cellfun (@(x,y,z) x + y + z, {1, 1, 1}, {2, 2, 2}, {3, 4, 5}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
689 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
690 %! assert (A, {6, 7, 8}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
691 %!test %% Two input arguments of different types |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
692 %! A = cellfun (@(x,y) islogical (x) && ischar (y), {false, true}, {"a", 3}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
693 %! assert (A, [true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
694 %!test %% Pass another variable to the anonymous function |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
695 %! y = true; A = cellfun (@(x) islogical (x) && y, {false, 0.3}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
696 %! assert (A, [true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
697 %!test %% Three ouptut arguments of different type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
698 %! [A, B, C] = cellfun (@find, {10, 11; 0, 12}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
699 %! assert (isequal (A, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
700 %! assert (isequal (B, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
701 %! assert (isequal (C, {10, 11; [], 12})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
702 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
703 %% Input arguments can be of type cell array of logical |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
704 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
705 %! A = cellfun (@(x,y) x == y, {false, true}, {true, true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
706 %! assert (A, [false, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
707 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
708 %! A = cellfun (@(x,y) x == y, {false; true}, {true; true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
709 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
710 %! assert (A, [false; true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
711 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
712 %! A = cellfun (@(x) x, {false, true; false, true}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
713 %! assert (A, {false, true; false, true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
714 %!test %% Three ouptut arguments of same type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
715 %! [A, B, C] = cellfun (@find, {true, false; false, true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
716 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
717 %! assert (isequal (A, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
718 %! assert (isequal (B, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
719 %! assert (isequal (C, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
720 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
721 %! A = cellfun (@(x,y) cell2str (x,y), {true}, {true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
722 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
723 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
724 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
725 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
726 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
727 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
728 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
729 %! A = cellfun (@(x,y) cell2str (x,y), {true}, {true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
730 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
731 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
732 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
733 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
734 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
735 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
736 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
737 %% Input arguments can be of type cell array of numeric |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
738 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
739 %! A = cellfun (@(x,y) x>y, {1.1, 4.2}, {3.1, 2+6*i}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
740 %! assert (A, [false, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
741 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
742 %! A = cellfun (@(x,y) x>y, {1.1, 4.2; 2, 4}, {3.1, 2; 2, 4+2*i}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
743 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
744 %! assert (A, [false, true; false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
745 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
746 %! A = cellfun (@(x,y) x:y, {1.1, 4}, {3.1, 6}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
747 %! assert (isequal (A{1}, [1.1, 2.1, 3.1])); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
748 %! assert (isequal (A{2}, [4, 5, 6])); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
749 %!test %% Three ouptut arguments of different type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
750 %! [A, B, C] = cellfun (@find, {10, 11; 0, 12}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
751 %! assert (isequal (A, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
752 %! assert (isequal (B, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
753 %! assert (isequal (C, {10, 11; [], 12})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
754 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
755 %! A = cellfun (@(x,y) cell2str(x,y), {1.1, 4}, {3.1, 6}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
756 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
757 %! B = isfield (A(1), "message") && isfield (A(1), "index"); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
758 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
759 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
760 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
761 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
762 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
763 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
764 %! A = cellfun (@(x,y) cell2str(x,y), {1.1, 4}, {3.1, 6}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
765 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
766 %! B = isfield (A(1), "message") && isfield (A(1), "index"); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
767 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
768 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
769 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
770 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
771 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
772 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
773 %% Input arguments can be of type cell arrays of character or strings |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
774 %!error %% "UniformOutput" false should be used |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
775 %! A = cellfun (@(x,y) x>y, {"ad", "c", "ghi"}, {"cc", "d", "fgh"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
776 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
777 %! A = cellfun (@(x,y) x>y, {"a"; "f"}, {"c"; "d"}, "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
778 %! assert (A, [false; true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
779 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
780 %! A = cellfun (@(x,y) x:y, {"a", "d"}, {"c", "f"}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
781 %! assert (A, {"abc", "def"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
782 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
783 %! A = cellfun (@(x,y) cell2str(x,y), {"a", "d"}, {"c", "f"}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
784 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
785 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
786 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
787 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
788 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
789 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
790 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
791 %! A = cellfun (@(x,y) cell2str(x,y), {"a", "d"}, {"c", "f"}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
792 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
793 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
794 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
795 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
796 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
797 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
798 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
799 %% Structures cannot be handled by cellfun |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
800 %!error |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
801 %! vst1.a = 1.1; vst1.b = 4.2; vst2.a = 3.1; vst2.b = 2; |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
802 %! A = cellfun (@(x,y) (x.a < y.a) && (x.b > y.b), vst1, vst2); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
803 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
804 %% Input arguments can be of type cell array of cell arrays |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
805 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
806 %! A = cellfun (@(x,y) x{1} < y{1}, {{1.1}, {4.2}}, {{3.1}, {2}}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
807 %! assert (A, [1, 0], 1e-16); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
808 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
809 %! A = cellfun (@(x,y) x{1} < y{1}, {{1.1}; {4.2}}, {{3.1}; {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
810 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
811 %! assert (A, [1; 0], 1e-16); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
812 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
813 %! A = cellfun (@(x,y) x{1} < y{1}, {{1.1}, {4.2}}, {{3.1}, {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
814 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
815 %! assert (A, {true, false}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
816 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
817 %! A = cellfun (@(x,y) mat2str(x,y), {{1.1}, {4.2}}, {{3.1}, {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
818 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
819 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
820 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
821 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
822 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
823 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
824 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
825 %! A = cellfun (@(x,y) mat2str(x,y), {{1.1}, {4.2}}, {{3.1}, {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
826 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
827 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
828 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
829 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
830 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
831 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
832 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
833 %% Input arguments can be of type cell array of structure arrays |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
834 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
835 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
836 %! A = cellfun (@(x,y) (x.a == y.a) && (x.b < y.b), {a}, {b}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
837 %! assert (A, true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
838 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
839 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
840 %! A = cellfun (@(x,y) (x.a == y.a) && (x.b < y.b) , {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
841 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
842 %! assert (A, true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
843 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
844 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
845 %! A = cellfun (@(x,y) (x.a == y.a) && (x.b < y.b) , {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
846 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
847 %! assert (A, {true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
848 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
849 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
850 %! A = cellfun (@(x,y) cell2str (x.a, y.a), {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
851 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
852 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
853 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
854 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
855 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
856 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
857 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
858 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
859 %! A = cellfun (@(x,y) cell2str (x.a, y.a), {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
860 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
861 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
862 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
863 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
864 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
865 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
866 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
867 %% A lot of other tests |
5988 | 868 %!error(cellfun(1)) |
869 %!error(cellfun('isclass',1)) | |
870 %!error(cellfun('size',1)) | |
871 %!error(cellfun(@sin,{[]},'BadParam',false)) | |
872 %!error(cellfun(@sin,{[]},'UniformOuput')) | |
873 %!error(cellfun(@sin,{[]},'ErrorHandler')) | |
874 %!assert(cellfun(@sin,{0,1}),sin([0,1])) | |
875 %!assert(cellfun(inline('sin(x)'),{0,1}),sin([0,1])) | |
876 %!assert(cellfun('sin',{0,1}),sin([0,1])) | |
877 %!assert(cellfun('isempty',{1,[]}),[false,true]) | |
878 %!assert(cellfun('islogical',{false,pi}),[true,false]) | |
879 %!assert(cellfun('isreal',{1i,1}),[false,true]) | |
880 %!assert(cellfun('length',{zeros(2,2),1}),[2,1]) | |
881 %!assert(cellfun('prodofsize',{zeros(2,2),1}),[4,1]) | |
882 %!assert(cellfun('ndims',{zeros([2,2,2]),1}),[3,2]) | |
883 %!assert(cellfun('isclass',{zeros([2,2,2]),'test'},'double'),[true,false]) | |
884 %!assert(cellfun('size',{zeros([1,2,3]),1},1),[1,1]) | |
885 %!assert(cellfun('size',{zeros([1,2,3]),1},2),[2,1]) | |
886 %!assert(cellfun('size',{zeros([1,2,3]),1},3),[3,1]) | |
887 %!assert(cellfun(@atan2,{1,1},{1,2}),[atan2(1,1),atan2(1,2)]) | |
888 %!assert(cellfun(@atan2,{1,1},{1,2},'UniformOutput',false),{atan2(1,1),atan2(1,2)}) | |
6422 | 889 %!assert(cellfun(@sin,{1,2;3,4}),sin([1,2;3,4])) |
890 %!assert(cellfun(@atan2,{1,1;1,1},{1,2;1,2}),atan2([1,1;1,1],[1,2;1,2])) | |
5988 | 891 %!error(cellfun(@factorial,{-1,3})) |
892 %!assert(cellfun(@factorial,{-1,3},'ErrorHandler',@(x,y) NaN),[NaN,6]) | |
893 %!test | |
6704 | 894 %! [a,b,c]=cellfun(@fileparts,{fullfile("a","b","c.d"),fullfile("e","f","g.h")},'UniformOutput',false); |
895 %! assert(a,{fullfile("a","b"),fullfile("e","f")}) | |
5988 | 896 %! assert(b,{'c','g'}) |
897 %! assert(c,{'.d','.h'}) | |
898 | |
899 */ | |
900 | |
5451 | 901 DEFUN_DLD (num2cell, args, , |
902 "-*- texinfo -*-\n\ | |
903 @deftypefn {Loadable Function} {@var{c} =} num2cell (@var{m})\n\ | |
8095
d94a597c94a2
Typo in num2cell's description
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7977
diff
changeset
|
904 @deftypefnx {Loadable Function} {@var{c} =} num2cell (@var{m}, @var{dim})\n\ |
8096 | 905 Convert the matrix @var{m} into a cell array. If @var{dim} is defined, the\n\ |
5451 | 906 value @var{c} is of dimension 1 in this dimension and the elements of\n\ |
907 @var{m} are placed in slices in @var{c}.\n\ | |
5642 | 908 @seealso{mat2cell}\n\ |
909 @end deftypefn") | |
5451 | 910 { |
911 int nargin = args.length(); | |
912 octave_value retval; | |
913 | |
914 if (nargin < 1 || nargin > 2) | |
5823 | 915 print_usage (); |
5451 | 916 else |
917 { | |
918 dim_vector dv = args(0).dims (); | |
919 Array<int> sings; | |
920 | |
921 if (nargin == 2) | |
922 { | |
923 ColumnVector dsings = ColumnVector (args(1).vector_value | |
924 (false, true)); | |
925 sings.resize (dsings.length()); | |
926 | |
927 if (!error_state) | |
5988 | 928 for (octave_idx_type i = 0; i < dsings.length(); i++) |
5451 | 929 if (dsings(i) > dv.length() || dsings(i) < 1 || |
930 D_NINT(dsings(i)) != dsings(i)) | |
931 { | |
932 error ("invalid dimension specified"); | |
933 break; | |
934 } | |
935 else | |
936 sings(i) = NINT(dsings(i)) - 1; | |
937 } | |
938 | |
939 if (! error_state) | |
940 { | |
941 Array<bool> idx_colon (dv.length()); | |
942 dim_vector new_dv (dv); | |
943 octave_value_list lst (new_dv.length(), octave_value()); | |
944 | |
945 for (int i = 0; i < dv.length(); i++) | |
946 { | |
947 idx_colon(i) = false; | |
948 for (int j = 0; j < sings.length(); j++) | |
949 { | |
950 if (sings(j) == i) | |
951 { | |
952 new_dv(i) = 1; | |
953 idx_colon(i) = true; | |
954 lst(i) = octave_value (octave_value::magic_colon_t); | |
955 break; | |
956 } | |
957 } | |
958 } | |
959 | |
960 Cell ret (new_dv); | |
961 octave_idx_type nel = new_dv.numel(); | |
962 octave_idx_type ntot = 1; | |
963 | |
964 for (int j = 0; j < new_dv.length()-1; j++) | |
965 ntot *= new_dv(j); | |
966 | |
967 for (octave_idx_type i = 0; i < nel; i++) | |
968 { | |
969 octave_idx_type n = ntot; | |
970 octave_idx_type ii = i; | |
971 for (int j = new_dv.length() - 1; j >= 0 ; j--) | |
972 { | |
973 if (! idx_colon(j)) | |
974 lst (j) = ii/n + 1; | |
975 ii = ii % n; | |
976 if (j != 0) | |
977 n /= new_dv(j-1); | |
978 } | |
979 ret(i) = args(0).do_index_op(lst, 0); | |
980 } | |
981 | |
982 retval = ret; | |
983 } | |
984 } | |
985 | |
986 return retval; | |
987 } | |
988 | |
5988 | 989 /* |
990 | |
991 %!assert(num2cell([1,2;3,4]),{1,2;3,4}) | |
992 %!assert(num2cell([1,2;3,4],1),{[1;3],[2;4]}) | |
993 %!assert(num2cell([1,2;3,4],2),{[1,2];[3,4]}) | |
994 | |
995 */ | |
996 | |
5784 | 997 DEFUN_DLD (mat2cell, args, , |
998 "-*- texinfo -*-\n\ | |
999 @deftypefn {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{m}, @var{n})\n\ | |
1000 @deftypefnx {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{d1}, @var{d2}, @dots{})\n\ | |
1001 @deftypefnx {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{r})\n\ | |
8096 | 1002 Convert the matrix @var{a} to a cell array. If @var{a} is 2-D, then\n\ |
5784 | 1003 it is required that @code{sum (@var{m}) == size (@var{a}, 1)} and\n\ |
1004 @code{sum (@var{n}) == size (@var{a}, 2)}. Similarly, if @var{a} is\n\ | |
1005 a multi-dimensional and the number of dimensional arguments is equal\n\ | |
1006 to the dimensions of @var{a}, then it is required that @code{sum (@var{di})\n\ | |
1007 == size (@var{a}, i)}.\n\ | |
1008 \n\ | |
1009 Given a single dimensional argument @var{r}, the other dimensional\n\ | |
1010 arguments are assumed to equal @code{size (@var{a},@var{i})}.\n\ | |
1011 \n\ | |
1012 An example of the use of mat2cell is\n\ | |
1013 \n\ | |
1014 @example\n\ | |
1015 @group\n\ | |
1016 mat2cell (reshape(1:16,4,4),[3,1],[3,1])\n\ | |
1017 @result{} @{\n\ | |
1018 [1,1] =\n\ | |
1019 \n\ | |
1020 1 5 9\n\ | |
1021 2 6 10\n\ | |
1022 3 7 11\n\ | |
1023 \n\ | |
1024 [2,1] =\n\ | |
1025 \n\ | |
1026 4 8 12\n\ | |
1027 \n\ | |
1028 [1,2] =\n\ | |
1029 \n\ | |
1030 13\n\ | |
1031 14\n\ | |
1032 15\n\ | |
1033 \n\ | |
1034 [2,2] = 16\n\ | |
1035 @}\n\ | |
1036 @end group\n\ | |
1037 @end example\n\ | |
5829 | 1038 @seealso{num2cell, cell2mat}\n\ |
5784 | 1039 @end deftypefn") |
1040 { | |
1041 int nargin = args.length(); | |
1042 octave_value retval; | |
1043 | |
1044 if (nargin < 2) | |
7038 | 1045 print_usage (); |
5784 | 1046 else |
1047 { | |
1048 dim_vector dv = args(0).dims(); | |
1049 dim_vector new_dv; | |
1050 new_dv.resize(dv.length()); | |
1051 | |
1052 if (nargin > 2) | |
1053 { | |
1054 octave_idx_type nmax = -1; | |
1055 | |
1056 if (nargin - 1 != dv.length()) | |
1057 error ("mat2cell: Incorrect number of dimensions"); | |
1058 else | |
1059 { | |
1060 for (octave_idx_type j = 0; j < dv.length(); j++) | |
1061 { | |
1062 ColumnVector d = ColumnVector (args(j+1).vector_value | |
1063 (false, true)); | |
1064 | |
1065 if (d.length() < 1) | |
1066 { | |
1067 error ("mat2cell: dimension can not be empty"); | |
1068 break; | |
1069 } | |
1070 else | |
1071 { | |
1072 if (nmax < d.length()) | |
1073 nmax = d.length(); | |
1074 | |
1075 for (octave_idx_type i = 1; i < d.length(); i++) | |
1076 { | |
1077 OCTAVE_QUIT; | |
1078 | |
1079 if (d(i) >= 0) | |
1080 d(i) += d(i-1); | |
1081 else | |
1082 { | |
1083 error ("mat2cell: invalid dimensional argument"); | |
1084 break; | |
1085 } | |
1086 } | |
1087 | |
1088 if (d(0) < 0) | |
1089 error ("mat2cell: invalid dimensional argument"); | |
1090 | |
1091 if (d(d.length() - 1) != dv(j)) | |
1092 error ("mat2cell: inconsistent dimensions"); | |
1093 | |
1094 if (error_state) | |
1095 break; | |
1096 | |
1097 new_dv(j) = d.length(); | |
1098 } | |
1099 } | |
1100 } | |
1101 | |
1102 if (! error_state) | |
1103 { | |
1104 // Construct a matrix with the index values | |
1105 Matrix dimargs(nmax, new_dv.length()); | |
1106 for (octave_idx_type j = 0; j < new_dv.length(); j++) | |
1107 { | |
1108 OCTAVE_QUIT; | |
1109 | |
1110 ColumnVector d = ColumnVector (args(j+1).vector_value | |
1111 (false, true)); | |
1112 | |
1113 dimargs(0,j) = d(0); | |
1114 for (octave_idx_type i = 1; i < d.length(); i++) | |
1115 dimargs(i,j) = dimargs(i-1,j) + d(i); | |
1116 } | |
1117 | |
1118 | |
1119 octave_value_list lst (new_dv.length(), octave_value()); | |
1120 Cell ret (new_dv); | |
1121 octave_idx_type nel = new_dv.numel(); | |
1122 octave_idx_type ntot = 1; | |
1123 | |
1124 for (int j = 0; j < new_dv.length()-1; j++) | |
1125 ntot *= new_dv(j); | |
1126 | |
1127 for (octave_idx_type i = 0; i < nel; i++) | |
1128 { | |
1129 octave_idx_type n = ntot; | |
1130 octave_idx_type ii = i; | |
1131 for (octave_idx_type j = new_dv.length() - 1; j >= 0; j--) | |
1132 { | |
1133 OCTAVE_QUIT; | |
1134 | |
1135 octave_idx_type idx = ii / n; | |
1136 lst (j) = Range((idx == 0 ? 1. : dimargs(idx-1,j)+1.), | |
1137 dimargs(idx,j)); | |
1138 ii = ii % n; | |
1139 if (j != 0) | |
1140 n /= new_dv(j-1); | |
1141 } | |
1142 ret(i) = args(0).do_index_op(lst, 0); | |
1143 if (error_state) | |
1144 break; | |
1145 } | |
1146 | |
1147 if (!error_state) | |
1148 retval = ret; | |
1149 } | |
1150 } | |
1151 else | |
1152 { | |
1153 ColumnVector d = ColumnVector (args(1).vector_value | |
1154 (false, true)); | |
1155 | |
1156 double sumd = 0.; | |
1157 for (octave_idx_type i = 0; i < d.length(); i++) | |
1158 { | |
1159 OCTAVE_QUIT; | |
1160 | |
1161 if (d(i) >= 0) | |
1162 sumd += d(i); | |
1163 else | |
1164 { | |
1165 error ("mat2cell: invalid dimensional argument"); | |
1166 break; | |
1167 } | |
1168 } | |
1169 | |
1170 if (sumd != dv(0)) | |
1171 error ("mat2cell: inconsistent dimensions"); | |
1172 | |
1173 new_dv(0) = d.length(); | |
1174 for (octave_idx_type i = 1; i < dv.length(); i++) | |
1175 new_dv(i) = 1; | |
1176 | |
1177 if (! error_state) | |
1178 { | |
1179 octave_value_list lst (new_dv.length(), octave_value()); | |
1180 Cell ret (new_dv); | |
1181 | |
1182 for (octave_idx_type i = 1; i < new_dv.length(); i++) | |
1183 lst (i) = Range (1., static_cast<double>(dv(i))); | |
1184 | |
1185 double idx = 0.; | |
1186 for (octave_idx_type i = 0; i < new_dv(0); i++) | |
1187 { | |
1188 OCTAVE_QUIT; | |
1189 | |
1190 lst(0) = Range(idx + 1., idx + d(i)); | |
1191 ret(i) = args(0).do_index_op(lst, 0); | |
1192 idx += d(i); | |
1193 if (error_state) | |
1194 break; | |
1195 } | |
1196 | |
1197 if (!error_state) | |
1198 retval = ret; | |
1199 } | |
1200 } | |
1201 } | |
1202 | |
1203 return retval; | |
1204 } | |
1205 | |
1206 /* | |
1207 | |
1208 %!test | |
1209 %! x = reshape(1:20,5,4); | |
1210 %! c = mat2cell(x,[3,2],[3,1]); | |
1211 %! assert(c,{[1,6,11;2,7,12;3,8,13],[16;17;18];[4,9,14;5,10,15],[19;20]}) | |
1212 | |
1213 %!test | |
1214 %! x = 'abcdefghij'; | |
1215 %! c = mat2cell(x,1,[0,4,2,0,4,0]); | |
1216 %! empty1by0str = resize('',1,0); | |
1217 %! assert(c,{empty1by0str,'abcd','ef',empty1by0str,'ghij',empty1by0str}) | |
1218 | |
1219 */ | |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1220 |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1221 template <class NDA> |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1222 Cell |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1223 do_cellslices_nda (const NDA& array, const idx_vector& lb, const idx_vector& ub) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1224 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1225 octave_idx_type n = lb.length (0); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1226 Cell retval (1, n); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1227 for (octave_idx_type i = 0; i < n && ! error_state; i++) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1228 retval(i) = array.index (idx_vector (lb(i), ub(i) + 1)); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1229 return retval; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1230 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1231 |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1232 DEFUN_DLD (cellslices, args, , |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1233 "-*- texinfo -*-\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1234 @deftypefn {Loadable Function} {@var{sl} =} cellslices (@var{x}, @var{lb}, @var{ub})\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1235 Given a vector @var{x}, this function produces a cell array of slices from the vector\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1236 determined by the index vectors @var{lb}, @var{ub}, for lower and upper bounds, respectively.\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1237 In other words, it is equivalent to the following code:\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1238 \n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1239 @example\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1240 n = length (lb);\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1241 sl = cell (1, n);\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1242 for i = 1:length (lb)\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1243 sl@{i@} = x(lb(i):ub(i));\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1244 endfor\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1245 @end example\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1246 \n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1247 If @var{X} is a matrix, linear indexing will be used.\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1248 @seealso{mat2cell}\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1249 @end deftypefn") |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1250 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1251 octave_value retval; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1252 int nargin = args.length (); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1253 if (nargin == 3) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1254 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1255 octave_value x = args(0); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1256 idx_vector lb = args(1).index_vector (), ub = args(2).index_vector (); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1257 if (! error_state) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1258 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1259 if (lb.is_colon () || ub.is_colon ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1260 error ("cellslices: invalid use of colon"); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1261 else if (lb.length (0) != ub.length (0)) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1262 error ("cellslices: the lengths of lb and ub must match"); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1263 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1264 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1265 Cell retcell; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1266 if (! x.is_sparse_type () && x.is_matrix_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1267 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1268 // specialize for some dense arrays. |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1269 if (x.is_bool_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1270 retcell = do_cellslices_nda (x.bool_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1271 else if (x.is_char_matrix ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1272 retcell = do_cellslices_nda (x.char_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1273 else if (x.is_complex_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1274 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1275 if (x.is_single_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1276 retcell = do_cellslices_nda (x.float_complex_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1277 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1278 retcell = do_cellslices_nda (x.complex_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1279 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1280 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1281 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1282 if (x.is_single_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1283 retcell = do_cellslices_nda (x.float_array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1284 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1285 retcell = do_cellslices_nda (x.array_value (), lb, ub); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1286 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1287 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1288 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1289 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1290 // generic code. |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1291 octave_idx_type n = lb.length (0); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1292 retcell = Cell (1, n); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1293 octave_value_list idx (1, octave_value ()); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1294 for (octave_idx_type i = 0; i < n && ! error_state; i++) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1295 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1296 idx(0) = Range (static_cast<double> (lb(i)) + 1, |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1297 static_cast<double> (ub(i)) + 1); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1298 retcell(i) = x.do_index_op (idx); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1299 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1300 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1301 if (! error_state) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1302 retval = retcell; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1303 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1304 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1305 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1306 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1307 print_usage (); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1308 |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1309 return retval; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
1310 } |
5784 | 1311 |
5205 | 1312 /* |
1313 ;;; Local Variables: *** | |
1314 ;;; mode: C++ *** | |
1315 ;;; End: *** | |
1316 */ |