Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/cellfun.cc @ 8828:8463d1a2e544
Doc fixes.
* 2]$$. => 2].$$
* @var{extrapval} => @var{extrapval}.
* call helloworld.oct => called @file{helloworld.oct}
* @itemize => @table @code
* shows. => shows:
* save => @code{save}
* @ref{Breakpoints} => @pxref{Breakpoints}
* add @noindent following example
* which is computed => and compute it
* clarify wording
* remove comma
* good => well
* set => number
* by writing => with the command
* has the option of directly calling => can call
* [-like-] {+of the right size,+}
* solvers => routines
* handle => test for
* add introductory section
* add following
* {+the+} [0..bitmax] => [0,bitmax]
* of the => with
* number => value
* add usual
* Besides when doing comparisons, logical => Logical {+also+}
* array comparison => array, comparisons
* param => parameter
* works very similar => is similar
* strings, => strings
* most simple => simplest
* easier => more easily
* like => as
* called => called,
* clarify wording
* you should simply type => use
* clarify wording
* means => way
* equally => also
* [-way much-] {+way+}
* add with mean value parameter given by the first argument, @var{l}
* add Functions described as @dfn{mapping functions} apply the given
operation to each element when given a matrix argument.
* in this brief introduction => here
* It is worth noticing => Note
* add following
* means => ways
author | Brian Gough <bjg@network-theory.co.uk> |
---|---|
date | Fri, 20 Feb 2009 11:17:01 -0500 |
parents | 954b6f69f51d |
children | eb63fbe60fab |
rev | line source |
---|---|
5205 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2005, 2006, 2007 Mohamed Kamoun |
5205 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
5205 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
5205 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <string> | |
5988 | 28 #include <vector> |
29 #include <list> | |
5205 | 30 |
31 #include "lo-mappers.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8373
diff
changeset
|
32 #include "oct-locbuf.h" |
5205 | 33 |
34 #include "Cell.h" | |
5988 | 35 #include "oct-map.h" |
5205 | 36 #include "defun-dld.h" |
37 #include "parse.h" | |
38 #include "variables.h" | |
5451 | 39 #include "ov-colon.h" |
5988 | 40 #include "unwind-prot.h" |
5205 | 41 |
5988 | 42 DEFUN_DLD (cellfun, args, nargout, |
8715 | 43 "-*- texinfo -*-\n\ |
6854 | 44 @deftypefn {Loadable Function} {} cellfun (@var{name}, @var{c})\n\ |
45 @deftypefnx {Loadable Function} {} cellfun (\"size\", @var{c}, @var{k})\n\ | |
46 @deftypefnx {Loadable Function} {} cellfun (\"isclass\", @var{c}, @var{class})\n\ | |
47 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{c})\n\ | |
48 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{c}, @var{d})\n\ | |
49 @deftypefnx {Loadable Function} {[@var{a}, @var{b}] =} cellfun (@dots{})\n\ | |
50 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'ErrorHandler', @var{errfunc})\n\ | |
51 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'UniformOutput', @var{val})\n\ | |
5205 | 52 \n\ |
53 Evaluate the function named @var{name} on the elements of the cell array\n\ | |
5543 | 54 @var{c}. Elements in @var{c} are passed on to the named function\n\ |
5205 | 55 individually. The function @var{name} can be one of the functions\n\ |
56 \n\ | |
57 @table @code\n\ | |
58 @item isempty\n\ | |
5543 | 59 Return 1 for empty elements.\n\ |
5205 | 60 @item islogical\n\ |
61 Return 1 for logical elements.\n\ | |
62 @item isreal\n\ | |
63 Return 1 for real elements.\n\ | |
64 @item length\n\ | |
65 Return a vector of the lengths of cell elements.\n\ | |
5543 | 66 @item ndims\n\ |
5205 | 67 Return the number of dimensions of each element.\n\ |
68 @item prodofsize\n\ | |
69 Return the product of dimensions of each element.\n\ | |
70 @item size\n\ | |
71 Return the size along the @var{k}-th dimension.\n\ | |
72 @item isclass\n\ | |
73 Return 1 for elements of @var{class}.\n\ | |
74 @end table\n\ | |
75 \n\ | |
76 Additionally, @code{cellfun} accepts an arbitrary function @var{func}\n\ | |
77 in the form of an inline function, function handle, or the name of a\n\ | |
5988 | 78 function (in a character string). In the case of a character string\n\ |
79 argument, the function must accept a single argument named @var{x}, and\n\ | |
80 it must return a string value. The function can take one or more arguments,\n\ | |
81 with the inputs args given by @var{c}, @var{d}, etc. Equally the function\n\ | |
82 can return one or more output arguments. For example\n\ | |
5205 | 83 \n\ |
84 @example\n\ | |
85 @group\n\ | |
5988 | 86 cellfun (@@atan2, @{1, 0@}, @{0, 1@})\n\ |
87 @result{}ans = [1.57080 0.00000]\n\ | |
88 @end group\n\ | |
89 @end example\n\ | |
90 \n\ | |
91 Note that the default output argument is an array of the same size as the\n\ | |
92 input arguments.\n\ | |
93 \n\ | |
8828 | 94 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
|
95 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
|
96 return value. If 'UniformOutput' is false, the outputs are concatenated in\n\ |
5988 | 97 a cell array. For example\n\ |
98 \n\ | |
99 @example\n\ | |
100 @group\n\ | |
6623 | 101 cellfun (\"tolower(x)\", @{\"Foo\", \"Bar\", \"FooBar\"@},\n\ |
102 \"UniformOutput\",false)\n\ | |
5205 | 103 @result{} ans = @{\"foo\", \"bar\", \"foobar\"@}\n\ |
104 @end group\n\ | |
105 @end example\n\ | |
5988 | 106 \n\ |
107 Given the parameter 'ErrorHandler', then @var{errfunc} defines a function to\n\ | |
108 call in case @var{func} generates an error. The form of the function is\n\ | |
109 \n\ | |
110 @example\n\ | |
111 function [@dots{}] = errfunc (@var{s}, @dots{})\n\ | |
112 @end example\n\ | |
113 \n\ | |
114 where there is an additional input argument to @var{errfunc} relative to\n\ | |
115 @var{func}, given by @var{s}. This is a structure with the elements\n\ | |
116 'identifier', 'message' and 'index', giving respectively the error\n\ | |
117 identifier, the error message, and the index into the input arguments\n\ | |
118 of the element that caused the error. For example\n\ | |
119 \n\ | |
120 @example\n\ | |
121 @group\n\ | |
122 function y = foo (s, x), y = NaN; endfunction\n\ | |
123 cellfun (@@factorial, @{-1,2@},'ErrorHandler',@@foo)\n\ | |
124 @result{} ans = [NaN 2]\n\ | |
125 @end group\n\ | |
126 @end example\n\ | |
127 \n\ | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8096
diff
changeset
|
128 @seealso{isempty, islogical, isreal, length, ndims, numel, size}\n\ |
5642 | 129 @end deftypefn") |
5205 | 130 { |
5988 | 131 octave_value_list retval; |
5205 | 132 std::string name = "function"; |
133 octave_function *func = 0; | |
134 int nargin = args.length (); | |
5988 | 135 nargout = (nargout < 1 ? 1 : nargout); |
5205 | 136 |
137 if (nargin < 2) | |
138 { | |
139 error ("cellfun: you must supply at least 2 arguments"); | |
5823 | 140 print_usage (); |
5205 | 141 return retval; |
142 } | |
143 | |
144 if (args(0).is_function_handle () || args(0).is_inline_function ()) | |
145 { | |
146 func = args(0).function_value (); | |
147 | |
148 if (error_state) | |
149 return retval; | |
150 } | |
151 else if (args(0).is_string ()) | |
152 name = args(0).string_value (); | |
153 else | |
154 { | |
5543 | 155 error ("cellfun: first argument must be a string or function handle"); |
5205 | 156 return retval; |
157 } | |
158 | |
159 if (! args(1).is_cell ()) | |
160 { | |
5543 | 161 error ("cellfun: second argument must be a cell array"); |
5205 | 162 |
163 return retval; | |
164 } | |
165 | |
166 Cell f_args = args(1).cell_value (); | |
167 | |
5988 | 168 octave_idx_type k = f_args.numel (); |
5205 | 169 |
170 if (name == "isempty") | |
171 { | |
172 boolNDArray result (f_args.dims ()); | |
5988 | 173 for (octave_idx_type count = 0; count < k ; count++) |
5543 | 174 result(count) = f_args.elem(count).is_empty (); |
5988 | 175 retval(0) = result; |
5205 | 176 } |
177 else if (name == "islogical") | |
178 { | |
179 boolNDArray result (f_args.dims ()); | |
5988 | 180 for (octave_idx_type count= 0; count < k ; count++) |
5205 | 181 result(count) = f_args.elem(count).is_bool_type (); |
5988 | 182 retval(0) = result; |
5205 | 183 } |
184 else if (name == "isreal") | |
185 { | |
186 boolNDArray result (f_args.dims ()); | |
5988 | 187 for (octave_idx_type count= 0; count < k ; count++) |
5205 | 188 result(count) = f_args.elem(count).is_real_type (); |
5988 | 189 retval(0) = result; |
5205 | 190 } |
191 else if (name == "length") | |
192 { | |
193 NDArray result (f_args.dims ()); | |
5988 | 194 for (octave_idx_type count= 0; count < k ; count++) |
5544 | 195 result(count) = static_cast<double> (f_args.elem(count).length ()); |
5988 | 196 retval(0) = result; |
5205 | 197 } |
198 else if (name == "ndims") | |
199 { | |
200 NDArray result (f_args.dims ()); | |
5988 | 201 for (octave_idx_type count = 0; count < k ; count++) |
5544 | 202 result(count) = static_cast<double> (f_args.elem(count).ndims ()); |
5988 | 203 retval(0) = result; |
5205 | 204 } |
205 else if (name == "prodofsize") | |
206 { | |
207 NDArray result (f_args.dims ()); | |
5988 | 208 for (octave_idx_type count = 0; count < k ; count++) |
5544 | 209 result(count) = static_cast<double> (f_args.elem(count).numel ()); |
5988 | 210 retval(0) = result; |
5205 | 211 } |
212 else if (name == "size") | |
213 { | |
214 if (nargin == 3) | |
215 { | |
216 int d = args(2).nint_value () - 1; | |
217 | |
218 if (d < 0) | |
7001 | 219 error ("cellfun: third argument must be a positive integer"); |
5205 | 220 |
6484 | 221 if (! error_state) |
5205 | 222 { |
223 NDArray result (f_args.dims ()); | |
5988 | 224 for (octave_idx_type count = 0; count < k ; count++) |
5205 | 225 { |
226 dim_vector dv = f_args.elem(count).dims (); | |
227 if (d < dv.length ()) | |
5544 | 228 result(count) = static_cast<double> (dv(d)); |
5205 | 229 else |
230 result(count) = 1.0; | |
231 } | |
5988 | 232 retval(0) = result; |
5205 | 233 } |
234 } | |
235 else | |
5543 | 236 error ("not enough arguments for `size'"); |
5205 | 237 } |
238 else if (name == "isclass") | |
239 { | |
240 if (nargin == 3) | |
241 { | |
242 std::string class_name = args(2).string_value(); | |
243 boolNDArray result (f_args.dims ()); | |
5988 | 244 for (octave_idx_type count = 0; count < k ; count++) |
5205 | 245 result(count) = (f_args.elem(count).class_name() == class_name); |
246 | |
5988 | 247 retval(0) = result; |
5205 | 248 } |
249 else | |
5543 | 250 error ("not enough arguments for `isclass'"); |
5205 | 251 } |
252 else | |
253 { | |
5988 | 254 unwind_protect::begin_frame ("Fcellfun"); |
255 unwind_protect_int (buffer_error_messages); | |
256 | |
5205 | 257 std::string fcn_name; |
258 | |
259 if (! func) | |
260 { | |
261 fcn_name = unique_symbol_name ("__cellfun_fcn_"); | |
262 std::string fname = "function y = "; | |
263 fname.append (fcn_name); | |
264 fname.append ("(x) y = "); | |
265 func = extract_function (args(0), "cellfun", fcn_name, fname, | |
266 "; endfunction"); | |
267 } | |
268 | |
269 if (! func) | |
270 error ("unknown function"); | |
271 else | |
272 { | |
5988 | 273 octave_value_list idx; |
274 octave_value_list inputlist; | |
6484 | 275 bool uniform_output = true; |
276 bool have_error_handler = false; | |
5988 | 277 std::string err_name; |
6484 | 278 octave_function *error_handler = 0; |
5988 | 279 int offset = 1; |
280 int i = 1; | |
281 OCTAVE_LOCAL_BUFFER (Cell, inputs, nargin); | |
5205 | 282 |
5988 | 283 while (i < nargin) |
5205 | 284 { |
5988 | 285 if (args(i).is_string()) |
286 { | |
287 std::string arg = args(i++).string_value(); | |
288 if (i == nargin) | |
289 { | |
290 error ("cellfun: parameter value is missing"); | |
291 goto cellfun_err; | |
292 } | |
293 | |
294 std::transform (arg.begin (), arg.end (), | |
295 arg.begin (), tolower); | |
296 | |
297 if (arg == "uniformoutput") | |
6484 | 298 uniform_output = args(i++).bool_value(); |
5988 | 299 else if (arg == "errorhandler") |
300 { | |
301 if (args(i).is_function_handle () || | |
302 args(i).is_inline_function ()) | |
303 { | |
6484 | 304 error_handler = args(i).function_value (); |
5205 | 305 |
5988 | 306 if (error_state) |
307 goto cellfun_err; | |
308 } | |
309 else if (args(i).is_string ()) | |
310 { | |
311 err_name = unique_symbol_name ("__cellfun_fcn_"); | |
312 std::string fname = "function y = "; | |
313 fname.append (fcn_name); | |
314 fname.append ("(x) y = "); | |
6484 | 315 error_handler = extract_function (args(i), "cellfun", |
316 err_name, fname, | |
317 "; endfunction"); | |
5988 | 318 } |
319 | |
6484 | 320 if (! error_handler) |
5988 | 321 goto cellfun_err; |
322 | |
6484 | 323 have_error_handler = true; |
5988 | 324 i++; |
325 } | |
326 else | |
327 { | |
328 error ("cellfun: unrecognized parameter %s", | |
329 arg.c_str()); | |
330 goto cellfun_err; | |
331 } | |
332 offset += 2; | |
333 } | |
334 else | |
335 { | |
336 inputs[i-offset] = args(i).cell_value (); | |
337 if (f_args.dims() != inputs[i-offset].dims()) | |
338 { | |
339 error ("cellfun: Dimension mismatch"); | |
340 goto cellfun_err; | |
341 | |
342 } | |
343 i++; | |
344 } | |
5205 | 345 } |
346 | |
5988 | 347 inputlist.resize(nargin-offset); |
348 | |
6484 | 349 if (have_error_handler) |
5988 | 350 buffer_error_messages++; |
351 | |
6484 | 352 if (uniform_output) |
5988 | 353 { |
354 retval.resize(nargout); | |
355 | |
356 for (octave_idx_type count = 0; count < k ; count++) | |
357 { | |
5989 | 358 for (int j = 0; j < nargin-offset; j++) |
359 inputlist(j) = inputs[j](count); | |
5988 | 360 |
361 octave_value_list tmp = feval (func, inputlist, nargout); | |
362 | |
6484 | 363 if (error_state && have_error_handler) |
5988 | 364 { |
365 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
|
366 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
|
367 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
|
368 msg.assign ("index", octave_value(double (count + static_cast<octave_idx_type>(1)))); |
5988 | 369 octave_value_list errlist = inputlist; |
370 errlist.prepend (msg); | |
371 buffer_error_messages--; | |
372 error_state = 0; | |
6484 | 373 tmp = feval (error_handler, errlist, nargout); |
5988 | 374 buffer_error_messages++; |
375 | |
376 if (error_state) | |
377 goto cellfun_err; | |
378 } | |
379 | |
380 if (tmp.length() < nargout) | |
381 { | |
382 error ("cellfun: too many output arguments"); | |
383 goto cellfun_err; | |
384 } | |
385 | |
386 if (error_state) | |
387 break; | |
388 | |
389 if (count == 0) | |
390 { | |
5989 | 391 for (int j = 0; j < nargout; j++) |
5988 | 392 { |
393 octave_value val; | |
5989 | 394 val = tmp(j); |
5988 | 395 |
396 if (error_state) | |
397 goto cellfun_err; | |
398 | |
6422 | 399 retval(j) = val.resize(f_args.dims()); |
5988 | 400 } |
401 } | |
402 else | |
403 { | |
404 idx(0) = octave_value (static_cast<double>(count+1)); | |
5989 | 405 for (int j = 0; j < nargout; j++) |
406 { | |
407 // FIXME -- need an easier way to express | |
408 // this test. | |
409 octave_value val = tmp(j); | |
410 | |
411 if (val.ndims () == 2 | |
412 && val.rows () == 1 && val.columns () == 1) | |
413 retval(j) = | |
414 retval(j).subsasgn ("(", | |
415 std::list<octave_value_list> | |
416 (1, idx(0)), val); | |
417 else | |
418 error ("cellfun: expecting all values to be scalars for UniformOutput = true"); | |
419 } | |
5988 | 420 } |
421 | |
422 if (error_state) | |
423 break; | |
424 } | |
425 } | |
426 else | |
427 { | |
428 OCTAVE_LOCAL_BUFFER (Cell, results, nargout); | |
5989 | 429 for (int j = 0; j < nargout; j++) |
430 results[j].resize(f_args.dims()); | |
5988 | 431 |
432 for (octave_idx_type count = 0; count < k ; count++) | |
433 { | |
5989 | 434 for (int j = 0; j < nargin-offset; j++) |
435 inputlist(j) = inputs[j](count); | |
5988 | 436 |
437 octave_value_list tmp = feval (func, inputlist, nargout); | |
438 | |
6484 | 439 if (error_state && have_error_handler) |
5988 | 440 { |
441 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
|
442 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
|
443 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
|
444 msg.assign ("index", octave_value(double (count + static_cast<octave_idx_type>(1)))); |
5988 | 445 octave_value_list errlist = inputlist; |
446 errlist.prepend (msg); | |
447 buffer_error_messages--; | |
448 error_state = 0; | |
6484 | 449 tmp = feval (error_handler, errlist, nargout); |
5988 | 450 buffer_error_messages++; |
451 | |
452 if (error_state) | |
453 goto cellfun_err; | |
454 } | |
455 | |
456 if (tmp.length() < nargout) | |
457 { | |
458 error ("cellfun: too many output arguments"); | |
459 goto cellfun_err; | |
460 } | |
461 | |
462 if (error_state) | |
463 break; | |
464 | |
465 | |
5989 | 466 for (int j = 0; j < nargout; j++) |
467 results[j](count) = tmp(j); | |
5988 | 468 } |
469 | |
470 retval.resize(nargout); | |
5989 | 471 for (int j = 0; j < nargout; j++) |
472 retval(j) = results[j]; | |
5988 | 473 } |
474 | |
475 cellfun_err: | |
476 if (error_state) | |
477 retval = octave_value_list(); | |
5205 | 478 |
479 if (! fcn_name.empty ()) | |
480 clear_function (fcn_name); | |
5988 | 481 |
482 if (! err_name.empty ()) | |
483 clear_function (err_name); | |
5205 | 484 } |
5988 | 485 |
486 unwind_protect::run_frame ("Fcellfun"); | |
5205 | 487 } |
488 | |
489 return retval; | |
490 } | |
491 | |
5988 | 492 /* |
493 | |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
494 %% Test function to check the "Errorhandler" option |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
495 %!function [z] = cellfunerror (S, varargin) |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
496 %! z = S; |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
497 %! endfunction |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
498 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
499 %% 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
|
500 %% a function_handle or an anonymous function |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
501 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
502 %! 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
|
503 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
504 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
505 %! 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
|
506 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
507 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
508 %! 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
|
509 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
510 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
511 %! 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
|
512 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
513 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
514 %% 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
|
515 %% "isempty", "islogical", "length", "ndims" or "prodofsize" |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
516 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
517 %! 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
|
518 %! assert (A, [true, true, true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
519 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
520 %! 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
|
521 %! assert (A, [false, false, false, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
522 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
523 %! 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
|
524 %! assert (A, [true, false, true, false, false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
525 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
526 %! 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
|
527 %! assert (A, [1, 1, 1, 1, 0, 3]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
528 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
529 %! 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
|
530 %! assert (A, [2; 4]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
531 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
532 %! 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
|
533 %! assert (A, [4, 24]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
534 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
535 %% 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
|
536 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
537 %! 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
|
538 %! assert (A, [6, 7, 8]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
539 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
540 %! 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
|
541 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
542 %! assert (A, {6, 7, 8}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
543 %!test %% Two input arguments of different types |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
544 %! 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
|
545 %! assert (A, [true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
546 %!test %% Pass another variable to the anonymous function |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
547 %! 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
|
548 %! assert (A, [true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
549 %!test %% Three ouptut arguments of different type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
550 %! [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
|
551 %! assert (isequal (A, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
552 %! assert (isequal (B, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
553 %! assert (isequal (C, {10, 11; [], 12})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
554 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
555 %% 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
|
556 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
557 %! 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
|
558 %! assert (A, [false, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
559 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
560 %! 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
|
561 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
562 %! assert (A, [false; true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
563 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
564 %! 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
|
565 %! assert (A, {false, true; false, true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
566 %!test %% Three ouptut arguments of same type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
567 %! [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
|
568 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
569 %! assert (isequal (A, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
570 %! assert (isequal (B, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
571 %! assert (isequal (C, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
572 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
573 %! 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
|
574 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
575 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
576 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
577 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
578 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
579 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
580 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
581 %! 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
|
582 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
583 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
584 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
585 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
586 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
587 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
588 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
589 %% 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
|
590 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
591 %! 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
|
592 %! assert (A, [false, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
593 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
594 %! 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
|
595 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
596 %! assert (A, [false, true; false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
597 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
598 %! 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
|
599 %! 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
|
600 %! assert (isequal (A{2}, [4, 5, 6])); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
601 %!test %% Three ouptut arguments of different type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
602 %! [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
|
603 %! assert (isequal (A, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
604 %! assert (isequal (B, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
605 %! assert (isequal (C, {10, 11; [], 12})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
606 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
607 %! 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
|
608 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
609 %! 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
|
610 %! 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
|
611 %! 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
|
612 %! 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
|
613 %! 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
|
614 %! 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
|
615 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
616 %! 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
|
617 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
618 %! 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
|
619 %! 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
|
620 %! 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
|
621 %! 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
|
622 %! 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
|
623 %! 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
|
624 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
625 %% 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
|
626 %!error %% "UniformOutput" false should be used |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
627 %! 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
|
628 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
629 %! 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
|
630 %! assert (A, [false; true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
631 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
632 %! 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
|
633 %! assert (A, {"abc", "def"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
634 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
635 %! 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
|
636 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
637 %! 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
|
638 %! 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
|
639 %! 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
|
640 %! 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
|
641 %! 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
|
642 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
643 %! 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
|
644 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
645 %! 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
|
646 %! 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
|
647 %! 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
|
648 %! 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
|
649 %! 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
|
650 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
651 %% Structures cannot be handled by cellfun |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
652 %!error |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
653 %! 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
|
654 %! 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
|
655 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
656 %% 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
|
657 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
658 %! 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
|
659 %! assert (A, [1, 0], 1e-16); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
660 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
661 %! 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
|
662 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
663 %! assert (A, [1; 0], 1e-16); |
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 (@(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
|
666 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
667 %! assert (A, {true, false}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
668 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
669 %! 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
|
670 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
671 %! 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
|
672 %! 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
|
673 %! 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
|
674 %! 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
|
675 %! 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
|
676 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
677 %! 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
|
678 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
679 %! 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
|
680 %! 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
|
681 %! 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
|
682 %! 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
|
683 %! 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
|
684 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
685 %% 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
|
686 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
687 %! 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
|
688 %! 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
|
689 %! assert (A, true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
690 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
691 %! 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
|
692 %! 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
|
693 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
694 %! assert (A, true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
695 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
696 %! 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
|
697 %! 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
|
698 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
699 %! assert (A, {true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
700 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
701 %! 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
|
702 %! 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
|
703 %! "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
704 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
705 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
706 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
707 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
708 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
709 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
710 %! 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
|
711 %! 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
|
712 %! "UniformOutput", true, "ErrorHandler", @cellfunerror); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
713 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
714 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
715 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
716 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
717 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
718 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
719 %% A lot of other tests |
5988 | 720 %!error(cellfun(1)) |
721 %!error(cellfun('isclass',1)) | |
722 %!error(cellfun('size',1)) | |
723 %!error(cellfun(@sin,{[]},'BadParam',false)) | |
724 %!error(cellfun(@sin,{[]},'UniformOuput')) | |
725 %!error(cellfun(@sin,{[]},'ErrorHandler')) | |
726 %!assert(cellfun(@sin,{0,1}),sin([0,1])) | |
727 %!assert(cellfun(inline('sin(x)'),{0,1}),sin([0,1])) | |
728 %!assert(cellfun('sin',{0,1}),sin([0,1])) | |
729 %!assert(cellfun('isempty',{1,[]}),[false,true]) | |
730 %!assert(cellfun('islogical',{false,pi}),[true,false]) | |
731 %!assert(cellfun('isreal',{1i,1}),[false,true]) | |
732 %!assert(cellfun('length',{zeros(2,2),1}),[2,1]) | |
733 %!assert(cellfun('prodofsize',{zeros(2,2),1}),[4,1]) | |
734 %!assert(cellfun('ndims',{zeros([2,2,2]),1}),[3,2]) | |
735 %!assert(cellfun('isclass',{zeros([2,2,2]),'test'},'double'),[true,false]) | |
736 %!assert(cellfun('size',{zeros([1,2,3]),1},1),[1,1]) | |
737 %!assert(cellfun('size',{zeros([1,2,3]),1},2),[2,1]) | |
738 %!assert(cellfun('size',{zeros([1,2,3]),1},3),[3,1]) | |
739 %!assert(cellfun(@atan2,{1,1},{1,2}),[atan2(1,1),atan2(1,2)]) | |
740 %!assert(cellfun(@atan2,{1,1},{1,2},'UniformOutput',false),{atan2(1,1),atan2(1,2)}) | |
6422 | 741 %!assert(cellfun(@sin,{1,2;3,4}),sin([1,2;3,4])) |
742 %!assert(cellfun(@atan2,{1,1;1,1},{1,2;1,2}),atan2([1,1;1,1],[1,2;1,2])) | |
5988 | 743 %!error(cellfun(@factorial,{-1,3})) |
744 %!assert(cellfun(@factorial,{-1,3},'ErrorHandler',@(x,y) NaN),[NaN,6]) | |
745 %!test | |
6704 | 746 %! [a,b,c]=cellfun(@fileparts,{fullfile("a","b","c.d"),fullfile("e","f","g.h")},'UniformOutput',false); |
747 %! assert(a,{fullfile("a","b"),fullfile("e","f")}) | |
5988 | 748 %! assert(b,{'c','g'}) |
749 %! assert(c,{'.d','.h'}) | |
750 | |
751 */ | |
752 | |
5451 | 753 DEFUN_DLD (num2cell, args, , |
754 "-*- texinfo -*-\n\ | |
755 @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
|
756 @deftypefnx {Loadable Function} {@var{c} =} num2cell (@var{m}, @var{dim})\n\ |
8096 | 757 Convert the matrix @var{m} into a cell array. If @var{dim} is defined, the\n\ |
5451 | 758 value @var{c} is of dimension 1 in this dimension and the elements of\n\ |
759 @var{m} are placed in slices in @var{c}.\n\ | |
5642 | 760 @seealso{mat2cell}\n\ |
761 @end deftypefn") | |
5451 | 762 { |
763 int nargin = args.length(); | |
764 octave_value retval; | |
765 | |
766 if (nargin < 1 || nargin > 2) | |
5823 | 767 print_usage (); |
5451 | 768 else |
769 { | |
770 dim_vector dv = args(0).dims (); | |
771 Array<int> sings; | |
772 | |
773 if (nargin == 2) | |
774 { | |
775 ColumnVector dsings = ColumnVector (args(1).vector_value | |
776 (false, true)); | |
777 sings.resize (dsings.length()); | |
778 | |
779 if (!error_state) | |
5988 | 780 for (octave_idx_type i = 0; i < dsings.length(); i++) |
5451 | 781 if (dsings(i) > dv.length() || dsings(i) < 1 || |
782 D_NINT(dsings(i)) != dsings(i)) | |
783 { | |
784 error ("invalid dimension specified"); | |
785 break; | |
786 } | |
787 else | |
788 sings(i) = NINT(dsings(i)) - 1; | |
789 } | |
790 | |
791 if (! error_state) | |
792 { | |
793 Array<bool> idx_colon (dv.length()); | |
794 dim_vector new_dv (dv); | |
795 octave_value_list lst (new_dv.length(), octave_value()); | |
796 | |
797 for (int i = 0; i < dv.length(); i++) | |
798 { | |
799 idx_colon(i) = false; | |
800 for (int j = 0; j < sings.length(); j++) | |
801 { | |
802 if (sings(j) == i) | |
803 { | |
804 new_dv(i) = 1; | |
805 idx_colon(i) = true; | |
806 lst(i) = octave_value (octave_value::magic_colon_t); | |
807 break; | |
808 } | |
809 } | |
810 } | |
811 | |
812 Cell ret (new_dv); | |
813 octave_idx_type nel = new_dv.numel(); | |
814 octave_idx_type ntot = 1; | |
815 | |
816 for (int j = 0; j < new_dv.length()-1; j++) | |
817 ntot *= new_dv(j); | |
818 | |
819 for (octave_idx_type i = 0; i < nel; i++) | |
820 { | |
821 octave_idx_type n = ntot; | |
822 octave_idx_type ii = i; | |
823 for (int j = new_dv.length() - 1; j >= 0 ; j--) | |
824 { | |
825 if (! idx_colon(j)) | |
826 lst (j) = ii/n + 1; | |
827 ii = ii % n; | |
828 if (j != 0) | |
829 n /= new_dv(j-1); | |
830 } | |
831 ret(i) = args(0).do_index_op(lst, 0); | |
832 } | |
833 | |
834 retval = ret; | |
835 } | |
836 } | |
837 | |
838 return retval; | |
839 } | |
840 | |
5988 | 841 /* |
842 | |
843 %!assert(num2cell([1,2;3,4]),{1,2;3,4}) | |
844 %!assert(num2cell([1,2;3,4],1),{[1;3],[2;4]}) | |
845 %!assert(num2cell([1,2;3,4],2),{[1,2];[3,4]}) | |
846 | |
847 */ | |
848 | |
5784 | 849 DEFUN_DLD (mat2cell, args, , |
850 "-*- texinfo -*-\n\ | |
851 @deftypefn {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{m}, @var{n})\n\ | |
852 @deftypefnx {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{d1}, @var{d2}, @dots{})\n\ | |
853 @deftypefnx {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{r})\n\ | |
8096 | 854 Convert the matrix @var{a} to a cell array. If @var{a} is 2-D, then\n\ |
5784 | 855 it is required that @code{sum (@var{m}) == size (@var{a}, 1)} and\n\ |
856 @code{sum (@var{n}) == size (@var{a}, 2)}. Similarly, if @var{a} is\n\ | |
857 a multi-dimensional and the number of dimensional arguments is equal\n\ | |
858 to the dimensions of @var{a}, then it is required that @code{sum (@var{di})\n\ | |
859 == size (@var{a}, i)}.\n\ | |
860 \n\ | |
861 Given a single dimensional argument @var{r}, the other dimensional\n\ | |
862 arguments are assumed to equal @code{size (@var{a},@var{i})}.\n\ | |
863 \n\ | |
864 An example of the use of mat2cell is\n\ | |
865 \n\ | |
866 @example\n\ | |
867 @group\n\ | |
868 mat2cell (reshape(1:16,4,4),[3,1],[3,1])\n\ | |
869 @result{} @{\n\ | |
870 [1,1] =\n\ | |
871 \n\ | |
872 1 5 9\n\ | |
873 2 6 10\n\ | |
874 3 7 11\n\ | |
875 \n\ | |
876 [2,1] =\n\ | |
877 \n\ | |
878 4 8 12\n\ | |
879 \n\ | |
880 [1,2] =\n\ | |
881 \n\ | |
882 13\n\ | |
883 14\n\ | |
884 15\n\ | |
885 \n\ | |
886 [2,2] = 16\n\ | |
887 @}\n\ | |
888 @end group\n\ | |
889 @end example\n\ | |
5829 | 890 @seealso{num2cell, cell2mat}\n\ |
5784 | 891 @end deftypefn") |
892 { | |
893 int nargin = args.length(); | |
894 octave_value retval; | |
895 | |
896 if (nargin < 2) | |
7038 | 897 print_usage (); |
5784 | 898 else |
899 { | |
900 dim_vector dv = args(0).dims(); | |
901 dim_vector new_dv; | |
902 new_dv.resize(dv.length()); | |
903 | |
904 if (nargin > 2) | |
905 { | |
906 octave_idx_type nmax = -1; | |
907 | |
908 if (nargin - 1 != dv.length()) | |
909 error ("mat2cell: Incorrect number of dimensions"); | |
910 else | |
911 { | |
912 for (octave_idx_type j = 0; j < dv.length(); j++) | |
913 { | |
914 ColumnVector d = ColumnVector (args(j+1).vector_value | |
915 (false, true)); | |
916 | |
917 if (d.length() < 1) | |
918 { | |
919 error ("mat2cell: dimension can not be empty"); | |
920 break; | |
921 } | |
922 else | |
923 { | |
924 if (nmax < d.length()) | |
925 nmax = d.length(); | |
926 | |
927 for (octave_idx_type i = 1; i < d.length(); i++) | |
928 { | |
929 OCTAVE_QUIT; | |
930 | |
931 if (d(i) >= 0) | |
932 d(i) += d(i-1); | |
933 else | |
934 { | |
935 error ("mat2cell: invalid dimensional argument"); | |
936 break; | |
937 } | |
938 } | |
939 | |
940 if (d(0) < 0) | |
941 error ("mat2cell: invalid dimensional argument"); | |
942 | |
943 if (d(d.length() - 1) != dv(j)) | |
944 error ("mat2cell: inconsistent dimensions"); | |
945 | |
946 if (error_state) | |
947 break; | |
948 | |
949 new_dv(j) = d.length(); | |
950 } | |
951 } | |
952 } | |
953 | |
954 if (! error_state) | |
955 { | |
956 // Construct a matrix with the index values | |
957 Matrix dimargs(nmax, new_dv.length()); | |
958 for (octave_idx_type j = 0; j < new_dv.length(); j++) | |
959 { | |
960 OCTAVE_QUIT; | |
961 | |
962 ColumnVector d = ColumnVector (args(j+1).vector_value | |
963 (false, true)); | |
964 | |
965 dimargs(0,j) = d(0); | |
966 for (octave_idx_type i = 1; i < d.length(); i++) | |
967 dimargs(i,j) = dimargs(i-1,j) + d(i); | |
968 } | |
969 | |
970 | |
971 octave_value_list lst (new_dv.length(), octave_value()); | |
972 Cell ret (new_dv); | |
973 octave_idx_type nel = new_dv.numel(); | |
974 octave_idx_type ntot = 1; | |
975 | |
976 for (int j = 0; j < new_dv.length()-1; j++) | |
977 ntot *= new_dv(j); | |
978 | |
979 for (octave_idx_type i = 0; i < nel; i++) | |
980 { | |
981 octave_idx_type n = ntot; | |
982 octave_idx_type ii = i; | |
983 for (octave_idx_type j = new_dv.length() - 1; j >= 0; j--) | |
984 { | |
985 OCTAVE_QUIT; | |
986 | |
987 octave_idx_type idx = ii / n; | |
988 lst (j) = Range((idx == 0 ? 1. : dimargs(idx-1,j)+1.), | |
989 dimargs(idx,j)); | |
990 ii = ii % n; | |
991 if (j != 0) | |
992 n /= new_dv(j-1); | |
993 } | |
994 ret(i) = args(0).do_index_op(lst, 0); | |
995 if (error_state) | |
996 break; | |
997 } | |
998 | |
999 if (!error_state) | |
1000 retval = ret; | |
1001 } | |
1002 } | |
1003 else | |
1004 { | |
1005 ColumnVector d = ColumnVector (args(1).vector_value | |
1006 (false, true)); | |
1007 | |
1008 double sumd = 0.; | |
1009 for (octave_idx_type i = 0; i < d.length(); i++) | |
1010 { | |
1011 OCTAVE_QUIT; | |
1012 | |
1013 if (d(i) >= 0) | |
1014 sumd += d(i); | |
1015 else | |
1016 { | |
1017 error ("mat2cell: invalid dimensional argument"); | |
1018 break; | |
1019 } | |
1020 } | |
1021 | |
1022 if (sumd != dv(0)) | |
1023 error ("mat2cell: inconsistent dimensions"); | |
1024 | |
1025 new_dv(0) = d.length(); | |
1026 for (octave_idx_type i = 1; i < dv.length(); i++) | |
1027 new_dv(i) = 1; | |
1028 | |
1029 if (! error_state) | |
1030 { | |
1031 octave_value_list lst (new_dv.length(), octave_value()); | |
1032 Cell ret (new_dv); | |
1033 | |
1034 for (octave_idx_type i = 1; i < new_dv.length(); i++) | |
1035 lst (i) = Range (1., static_cast<double>(dv(i))); | |
1036 | |
1037 double idx = 0.; | |
1038 for (octave_idx_type i = 0; i < new_dv(0); i++) | |
1039 { | |
1040 OCTAVE_QUIT; | |
1041 | |
1042 lst(0) = Range(idx + 1., idx + d(i)); | |
1043 ret(i) = args(0).do_index_op(lst, 0); | |
1044 idx += d(i); | |
1045 if (error_state) | |
1046 break; | |
1047 } | |
1048 | |
1049 if (!error_state) | |
1050 retval = ret; | |
1051 } | |
1052 } | |
1053 } | |
1054 | |
1055 return retval; | |
1056 } | |
1057 | |
1058 /* | |
1059 | |
1060 %!test | |
1061 %! x = reshape(1:20,5,4); | |
1062 %! c = mat2cell(x,[3,2],[3,1]); | |
1063 %! assert(c,{[1,6,11;2,7,12;3,8,13],[16;17;18];[4,9,14;5,10,15],[19;20]}) | |
1064 | |
1065 %!test | |
1066 %! x = 'abcdefghij'; | |
1067 %! c = mat2cell(x,1,[0,4,2,0,4,0]); | |
1068 %! empty1by0str = resize('',1,0); | |
1069 %! assert(c,{empty1by0str,'abcd','ef',empty1by0str,'ghij',empty1by0str}) | |
1070 | |
1071 */ | |
1072 | |
5205 | 1073 /* |
1074 ;;; Local Variables: *** | |
1075 ;;; mode: C++ *** | |
1076 ;;; End: *** | |
1077 */ |