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