5205
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 Mohamed Kamoun |
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
5205
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <string> |
5988
|
29 #include <vector> |
|
30 #include <list> |
5205
|
31 |
|
32 #include "lo-mappers.h" |
|
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 extern octave_value_list |
|
43 Flasterr (const octave_value_list& args_name, int nargout_name); |
|
44 |
|
45 DEFUN_DLD (cellfun, args, nargout, |
5205
|
46 " -*- texinfo -*-\n\ |
|
47 @deftypefn {Lodable Function} {} cellfun (@var{name}, @var{c})\n\ |
5543
|
48 @deftypefnx {Lodable Function} {} cellfun (\"size\", @var{c}, @var{k})\n\ |
5205
|
49 @deftypefnx {Lodable Function} {} cellfun (\"isclass\", @var{c}, @var{class})\n\ |
|
50 @deftypefnx {Lodable Function} {} cellfun (@var{func}, @var{c})\n\ |
5988
|
51 @deftypefnx {Lodable Function} {} cellfun (@var{func}, @var{c}, @var{d})\n\ |
|
52 @deftypefnx {Lodable Function} {[@var{a}, @var{b}]} = cellfun (@dots{})\n\ |
|
53 @deftypefnx {Lodable Function} {} cellfun (@dots{}, 'ErrorHandler',@var{errfunc})\n\ |
|
54 @deftypefnx {Lodable Function} {} cellfun (@dots{}, 'UniformOutput',@var{val})\n\ |
5205
|
55 \n\ |
|
56 Evaluate the function named @var{name} on the elements of the cell array\n\ |
5543
|
57 @var{c}. Elements in @var{c} are passed on to the named function\n\ |
5205
|
58 individually. The function @var{name} can be one of the functions\n\ |
|
59 \n\ |
|
60 @table @code\n\ |
|
61 @item isempty\n\ |
5543
|
62 Return 1 for empty elements.\n\ |
5205
|
63 @item islogical\n\ |
|
64 Return 1 for logical elements.\n\ |
|
65 @item isreal\n\ |
|
66 Return 1 for real elements.\n\ |
|
67 @item length\n\ |
|
68 Return a vector of the lengths of cell elements.\n\ |
5543
|
69 @item ndims\n\ |
5205
|
70 Return the number of dimensions of each element.\n\ |
|
71 @item prodofsize\n\ |
|
72 Return the product of dimensions of each element.\n\ |
|
73 @item size\n\ |
|
74 Return the size along the @var{k}-th dimension.\n\ |
|
75 @item isclass\n\ |
|
76 Return 1 for elements of @var{class}.\n\ |
|
77 @end table\n\ |
|
78 \n\ |
|
79 Additionally, @code{cellfun} accepts an arbitrary function @var{func}\n\ |
|
80 in the form of an inline function, function handle, or the name of a\n\ |
5988
|
81 function (in a character string). In the case of a character string\n\ |
|
82 argument, the function must accept a single argument named @var{x}, and\n\ |
|
83 it must return a string value. The function can take one or more arguments,\n\ |
|
84 with the inputs args given by @var{c}, @var{d}, etc. Equally the function\n\ |
|
85 can return one or more output arguments. For example\n\ |
5205
|
86 \n\ |
|
87 @example\n\ |
|
88 @group\n\ |
5988
|
89 cellfun (@@atan2, @{1, 0@}, @{0, 1@})\n\ |
|
90 @result{}ans = [1.57080 0.00000]\n\ |
|
91 @end group\n\ |
|
92 @end example\n\ |
|
93 \n\ |
|
94 Note that the default output argument is an array of the same size as the\n\ |
|
95 input arguments.\n\ |
|
96 \n\ |
|
97 If the param 'UniformOutput' is set to true (the default), then the function\n\ |
|
98 must return either a single element which will be concatenated into the\n\ |
|
99 return value. If 'UniformOutput is false, the outputs are concatenated in\n\ |
|
100 a cell array. For example\n\ |
|
101 \n\ |
|
102 @example\n\ |
|
103 @group\n\ |
|
104 cellfun (\"tolower(x)\", @{\"Foo\", \"Bar\", \"FooBar\"@},'UniformOutput',false)\n\ |
5205
|
105 @result{} ans = @{\"foo\", \"bar\", \"foobar\"@}\n\ |
|
106 @end group\n\ |
|
107 @end example\n\ |
5988
|
108 \n\ |
|
109 Given the parameter 'ErrorHandler', then @var{errfunc} defines a function to\n\ |
|
110 call in case @var{func} generates an error. The form of the function is\n\ |
|
111 \n\ |
|
112 @example\n\ |
|
113 function [@dots{}] = errfunc (@var{s}, @dots{})\n\ |
|
114 @end example\n\ |
|
115 \n\ |
|
116 where there is an additional input argument to @var{errfunc} relative to\n\ |
|
117 @var{func}, given by @var{s}. This is a structure with the elements\n\ |
|
118 'identifier', 'message' and 'index', giving respectively the error\n\ |
|
119 identifier, the error message, and the index into the input arguments\n\ |
|
120 of the element that caused the error. For example\n\ |
|
121 \n\ |
|
122 @example\n\ |
|
123 @group\n\ |
|
124 function y = foo (s, x), y = NaN; endfunction\n\ |
|
125 cellfun (@@factorial, @{-1,2@},'ErrorHandler',@@foo)\n\ |
|
126 @result{} ans = [NaN 2]\n\ |
|
127 @end group\n\ |
|
128 @end example\n\ |
|
129 \n\ |
5642
|
130 @seealso{isempty, islogical, isreal, length, ndims, numel, size, isclass}\n\ |
|
131 @end deftypefn") |
5205
|
132 { |
5988
|
133 octave_value_list retval; |
5205
|
134 std::string name = "function"; |
|
135 octave_function *func = 0; |
|
136 int nargin = args.length (); |
5988
|
137 nargout = (nargout < 1 ? 1 : nargout); |
5205
|
138 |
|
139 if (nargin < 2) |
|
140 { |
|
141 error ("cellfun: you must supply at least 2 arguments"); |
5823
|
142 print_usage (); |
5205
|
143 return retval; |
|
144 } |
|
145 |
|
146 if (args(0).is_function_handle () || args(0).is_inline_function ()) |
|
147 { |
|
148 func = args(0).function_value (); |
|
149 |
|
150 if (error_state) |
|
151 return retval; |
|
152 } |
|
153 else if (args(0).is_string ()) |
|
154 name = args(0).string_value (); |
|
155 else |
|
156 { |
5543
|
157 error ("cellfun: first argument must be a string or function handle"); |
5205
|
158 return retval; |
|
159 } |
|
160 |
|
161 if (! args(1).is_cell ()) |
|
162 { |
5543
|
163 error ("cellfun: second argument must be a cell array"); |
5205
|
164 |
|
165 return retval; |
|
166 } |
|
167 |
|
168 Cell f_args = args(1).cell_value (); |
|
169 |
5988
|
170 octave_idx_type k = f_args.numel (); |
5205
|
171 |
|
172 if (name == "isempty") |
|
173 { |
|
174 boolNDArray result (f_args.dims ()); |
5988
|
175 for (octave_idx_type count = 0; count < k ; count++) |
5543
|
176 result(count) = f_args.elem(count).is_empty (); |
5988
|
177 retval(0) = result; |
5205
|
178 } |
|
179 else if (name == "islogical") |
|
180 { |
|
181 boolNDArray result (f_args.dims ()); |
5988
|
182 for (octave_idx_type count= 0; count < k ; count++) |
5205
|
183 result(count) = f_args.elem(count).is_bool_type (); |
5988
|
184 retval(0) = result; |
5205
|
185 } |
|
186 else if (name == "isreal") |
|
187 { |
|
188 boolNDArray result (f_args.dims ()); |
5988
|
189 for (octave_idx_type count= 0; count < k ; count++) |
5205
|
190 result(count) = f_args.elem(count).is_real_type (); |
5988
|
191 retval(0) = result; |
5205
|
192 } |
|
193 else if (name == "length") |
|
194 { |
|
195 NDArray result (f_args.dims ()); |
5988
|
196 for (octave_idx_type count= 0; count < k ; count++) |
5544
|
197 result(count) = static_cast<double> (f_args.elem(count).length ()); |
5988
|
198 retval(0) = result; |
5205
|
199 } |
|
200 else if (name == "ndims") |
|
201 { |
|
202 NDArray result (f_args.dims ()); |
5988
|
203 for (octave_idx_type count = 0; count < k ; count++) |
5544
|
204 result(count) = static_cast<double> (f_args.elem(count).ndims ()); |
5988
|
205 retval(0) = result; |
5205
|
206 } |
|
207 else if (name == "prodofsize") |
|
208 { |
|
209 NDArray result (f_args.dims ()); |
5988
|
210 for (octave_idx_type count = 0; count < k ; count++) |
5544
|
211 result(count) = static_cast<double> (f_args.elem(count).numel ()); |
5988
|
212 retval(0) = result; |
5205
|
213 } |
|
214 else if (name == "size") |
|
215 { |
|
216 if (nargin == 3) |
|
217 { |
|
218 int d = args(2).nint_value () - 1; |
|
219 |
|
220 if (d < 0) |
|
221 error ("cellfun: third argument must be a postive integer"); |
|
222 |
|
223 if (!error_state) |
|
224 { |
|
225 NDArray result (f_args.dims ()); |
5988
|
226 for (octave_idx_type count = 0; count < k ; count++) |
5205
|
227 { |
|
228 dim_vector dv = f_args.elem(count).dims (); |
|
229 if (d < dv.length ()) |
5544
|
230 result(count) = static_cast<double> (dv(d)); |
5205
|
231 else |
|
232 result(count) = 1.0; |
|
233 } |
5988
|
234 retval(0) = result; |
5205
|
235 } |
|
236 } |
|
237 else |
5543
|
238 error ("not enough arguments for `size'"); |
5205
|
239 } |
|
240 else if (name == "isclass") |
|
241 { |
|
242 if (nargin == 3) |
|
243 { |
|
244 std::string class_name = args(2).string_value(); |
|
245 boolNDArray result (f_args.dims ()); |
5988
|
246 for (octave_idx_type count = 0; count < k ; count++) |
5205
|
247 result(count) = (f_args.elem(count).class_name() == class_name); |
|
248 |
5988
|
249 retval(0) = result; |
5205
|
250 } |
|
251 else |
5543
|
252 error ("not enough arguments for `isclass'"); |
5205
|
253 } |
|
254 else |
|
255 { |
5988
|
256 unwind_protect::begin_frame ("Fcellfun"); |
|
257 unwind_protect_int (buffer_error_messages); |
|
258 |
5205
|
259 std::string fcn_name; |
|
260 |
|
261 if (! func) |
|
262 { |
|
263 fcn_name = unique_symbol_name ("__cellfun_fcn_"); |
|
264 std::string fname = "function y = "; |
|
265 fname.append (fcn_name); |
|
266 fname.append ("(x) y = "); |
|
267 func = extract_function (args(0), "cellfun", fcn_name, fname, |
|
268 "; endfunction"); |
|
269 } |
|
270 |
|
271 if (! func) |
|
272 error ("unknown function"); |
|
273 else |
|
274 { |
5988
|
275 octave_value_list idx; |
|
276 octave_value_list inputlist; |
|
277 octave_value_list errlist; |
|
278 bool UniformOutput = true; |
|
279 bool haveErrorHandler = false; |
|
280 std::string err_name; |
|
281 octave_function *ErrorHandler; |
|
282 int offset = 1; |
|
283 int i = 1; |
|
284 OCTAVE_LOCAL_BUFFER (Cell, inputs, nargin); |
5205
|
285 |
5988
|
286 while (i < nargin) |
5205
|
287 { |
5988
|
288 if (args(i).is_string()) |
|
289 { |
|
290 std::string arg = args(i++).string_value(); |
|
291 if (i == nargin) |
|
292 { |
|
293 error ("cellfun: parameter value is missing"); |
|
294 goto cellfun_err; |
|
295 } |
|
296 |
|
297 std::transform (arg.begin (), arg.end (), |
|
298 arg.begin (), tolower); |
|
299 |
|
300 if (arg == "uniformoutput") |
|
301 UniformOutput = args(i++).bool_value(); |
|
302 else if (arg == "errorhandler") |
|
303 { |
|
304 if (args(i).is_function_handle () || |
|
305 args(i).is_inline_function ()) |
|
306 { |
|
307 ErrorHandler = args(i).function_value (); |
5205
|
308 |
5988
|
309 if (error_state) |
|
310 goto cellfun_err; |
|
311 } |
|
312 else if (args(i).is_string ()) |
|
313 { |
|
314 err_name = unique_symbol_name ("__cellfun_fcn_"); |
|
315 std::string fname = "function y = "; |
|
316 fname.append (fcn_name); |
|
317 fname.append ("(x) y = "); |
|
318 ErrorHandler = extract_function (args(i), "cellfun", |
|
319 err_name, fname, |
|
320 "; endfunction"); |
|
321 } |
|
322 |
|
323 if (!ErrorHandler) |
|
324 goto cellfun_err; |
|
325 |
|
326 haveErrorHandler = true; |
|
327 i++; |
|
328 } |
|
329 else |
|
330 { |
|
331 error ("cellfun: unrecognized parameter %s", |
|
332 arg.c_str()); |
|
333 goto cellfun_err; |
|
334 } |
|
335 offset += 2; |
|
336 } |
|
337 else |
|
338 { |
|
339 inputs[i-offset] = args(i).cell_value (); |
|
340 if (f_args.dims() != inputs[i-offset].dims()) |
|
341 { |
|
342 error ("cellfun: Dimension mismatch"); |
|
343 goto cellfun_err; |
|
344 |
|
345 } |
|
346 i++; |
|
347 } |
5205
|
348 } |
|
349 |
5988
|
350 inputlist.resize(nargin-offset); |
|
351 |
|
352 if (haveErrorHandler) |
|
353 buffer_error_messages++; |
|
354 |
|
355 if (UniformOutput) |
|
356 { |
|
357 retval.resize(nargout); |
|
358 |
|
359 for (octave_idx_type count = 0; count < k ; count++) |
|
360 { |
|
361 for (int i = 0; i < nargin-offset; i++) |
|
362 inputlist(i) = inputs[i](count); |
|
363 |
|
364 octave_value_list tmp = feval (func, inputlist, nargout); |
|
365 |
|
366 if (error_state && haveErrorHandler) |
|
367 { |
|
368 octave_value_list errtmp = |
|
369 Flasterr (octave_value_list (), 2); |
|
370 |
|
371 Octave_map msg; |
|
372 msg.assign ("identifier", errtmp(1)); |
|
373 msg.assign ("message", errtmp(0)); |
|
374 msg.assign ("index", octave_value(double (count))); |
|
375 octave_value_list errlist = inputlist; |
|
376 errlist.prepend (msg); |
|
377 buffer_error_messages--; |
|
378 error_state = 0; |
|
379 tmp = feval (ErrorHandler, errlist, nargout); |
|
380 buffer_error_messages++; |
|
381 |
|
382 if (error_state) |
|
383 goto cellfun_err; |
|
384 } |
|
385 |
|
386 if (tmp.length() < nargout) |
|
387 { |
|
388 error ("cellfun: too many output arguments"); |
|
389 goto cellfun_err; |
|
390 } |
|
391 |
|
392 if (error_state) |
|
393 break; |
|
394 |
|
395 if (count == 0) |
|
396 { |
|
397 for (int i = 0; i < nargout; i++) |
|
398 { |
|
399 octave_value val; |
|
400 val = tmp(i); |
|
401 |
|
402 if (error_state) |
|
403 goto cellfun_err; |
|
404 |
|
405 val.resize(f_args.dims()); |
|
406 retval(i) = val; |
|
407 } |
|
408 } |
|
409 else |
|
410 { |
|
411 idx(0) = octave_value (static_cast<double>(count+1)); |
|
412 for (int i = 0; i < nargout; i++) |
|
413 retval(i) = |
|
414 retval(i).subsasgn ("(", |
|
415 std::list<octave_value_list> |
|
416 (1, idx(0)), tmp(i)); |
|
417 } |
|
418 |
|
419 if (error_state) |
|
420 break; |
|
421 } |
|
422 } |
|
423 else |
|
424 { |
|
425 OCTAVE_LOCAL_BUFFER (Cell, results, nargout); |
|
426 for (int i = 0; i < nargout; i++) |
|
427 results[i].resize(f_args.dims()); |
|
428 |
|
429 for (octave_idx_type count = 0; count < k ; count++) |
|
430 { |
|
431 for (int i = 0; i < nargin-offset; i++) |
|
432 inputlist(i) = inputs[i](count); |
|
433 |
|
434 octave_value_list tmp = feval (func, inputlist, nargout); |
|
435 |
|
436 if (error_state && haveErrorHandler) |
|
437 { |
|
438 octave_value_list errtmp = |
|
439 Flasterr (octave_value_list (), 2); |
|
440 |
|
441 Octave_map msg; |
|
442 msg.assign ("identifier", errtmp(1)); |
|
443 msg.assign ("message", errtmp(0)); |
|
444 msg.assign ("index", octave_value(double (count))); |
|
445 octave_value_list errlist = inputlist; |
|
446 errlist.prepend (msg); |
|
447 buffer_error_messages--; |
|
448 error_state = 0; |
|
449 tmp = feval (ErrorHandler, errlist, nargout); |
|
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 |
|
466 for (int i = 0; i < nargout; i++) |
|
467 results[i](count) = tmp(i); |
|
468 } |
|
469 |
|
470 retval.resize(nargout); |
|
471 for (int i = 0; i < nargout; i++) |
|
472 retval(i) = results[i]; |
|
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 |
|
494 %!error(cellfun(1)) |
|
495 %!error(cellfun('isclass',1)) |
|
496 %!error(cellfun('size',1)) |
|
497 %!error(cellfun(@sin,{[]},'BadParam',false)) |
|
498 %!error(cellfun(@sin,{[]},'UniformOuput')) |
|
499 %!error(cellfun(@sin,{[]},'ErrorHandler')) |
|
500 %!assert(cellfun(@sin,{0,1}),sin([0,1])) |
|
501 %!assert(cellfun(inline('sin(x)'),{0,1}),sin([0,1])) |
|
502 %!assert(cellfun('sin',{0,1}),sin([0,1])) |
|
503 %!assert(cellfun('isempty',{1,[]}),[false,true]) |
|
504 %!assert(cellfun('islogical',{false,pi}),[true,false]) |
|
505 %!assert(cellfun('isreal',{1i,1}),[false,true]) |
|
506 %!assert(cellfun('length',{zeros(2,2),1}),[2,1]) |
|
507 %!assert(cellfun('prodofsize',{zeros(2,2),1}),[4,1]) |
|
508 %!assert(cellfun('ndims',{zeros([2,2,2]),1}),[3,2]) |
|
509 %!assert(cellfun('isclass',{zeros([2,2,2]),'test'},'double'),[true,false]) |
|
510 %!assert(cellfun('size',{zeros([1,2,3]),1},1),[1,1]) |
|
511 %!assert(cellfun('size',{zeros([1,2,3]),1},2),[2,1]) |
|
512 %!assert(cellfun('size',{zeros([1,2,3]),1},3),[3,1]) |
|
513 %!assert(cellfun(@atan2,{1,1},{1,2}),[atan2(1,1),atan2(1,2)]) |
|
514 %!assert(cellfun(@atan2,{1,1},{1,2},'UniformOutput',false),{atan2(1,1),atan2(1,2)}) |
|
515 %!error(cellfun(@factorial,{-1,3})) |
|
516 %!assert(cellfun(@factorial,{-1,3},'ErrorHandler',@(x,y) NaN),[NaN,6]) |
|
517 %!test |
|
518 %! [a,b,c]=cellfun(@fileparts,{'/a/b/c.d','/e/f/g.h'},'UniformOutput',false); |
|
519 %! assert(a,{'/a/b','/e/f'}) |
|
520 %! assert(b,{'c','g'}) |
|
521 %! assert(c,{'.d','.h'}) |
|
522 |
|
523 */ |
|
524 |
5451
|
525 DEFUN_DLD (num2cell, args, , |
|
526 "-*- texinfo -*-\n\ |
|
527 @deftypefn {Loadable Function} {@var{c} =} num2cell (@var{m})\n\ |
|
528 @deftypefnx {Loadable Function} {@var{c} =} num2cell (@var{m}, @var{d})\n\ |
|
529 Convert to matrix @var{m} into a cell array. If @var{d} is defined the\n\ |
|
530 value @var{c} is of dimension 1 in this dimension and the elements of\n\ |
|
531 @var{m} are placed in slices in @var{c}.\n\ |
5642
|
532 @seealso{mat2cell}\n\ |
|
533 @end deftypefn") |
5451
|
534 { |
|
535 int nargin = args.length(); |
|
536 octave_value retval; |
|
537 |
|
538 if (nargin < 1 || nargin > 2) |
5823
|
539 print_usage (); |
5451
|
540 else |
|
541 { |
|
542 dim_vector dv = args(0).dims (); |
|
543 Array<int> sings; |
|
544 |
|
545 if (nargin == 2) |
|
546 { |
|
547 ColumnVector dsings = ColumnVector (args(1).vector_value |
|
548 (false, true)); |
|
549 sings.resize (dsings.length()); |
|
550 |
|
551 if (!error_state) |
5988
|
552 for (octave_idx_type i = 0; i < dsings.length(); i++) |
5451
|
553 if (dsings(i) > dv.length() || dsings(i) < 1 || |
|
554 D_NINT(dsings(i)) != dsings(i)) |
|
555 { |
|
556 error ("invalid dimension specified"); |
|
557 break; |
|
558 } |
|
559 else |
|
560 sings(i) = NINT(dsings(i)) - 1; |
|
561 } |
|
562 |
|
563 if (! error_state) |
|
564 { |
|
565 Array<bool> idx_colon (dv.length()); |
|
566 dim_vector new_dv (dv); |
|
567 octave_value_list lst (new_dv.length(), octave_value()); |
|
568 |
|
569 for (int i = 0; i < dv.length(); i++) |
|
570 { |
|
571 idx_colon(i) = false; |
|
572 for (int j = 0; j < sings.length(); j++) |
|
573 { |
|
574 if (sings(j) == i) |
|
575 { |
|
576 new_dv(i) = 1; |
|
577 idx_colon(i) = true; |
|
578 lst(i) = octave_value (octave_value::magic_colon_t); |
|
579 break; |
|
580 } |
|
581 } |
|
582 } |
|
583 |
|
584 Cell ret (new_dv); |
|
585 octave_idx_type nel = new_dv.numel(); |
|
586 octave_idx_type ntot = 1; |
|
587 |
|
588 for (int j = 0; j < new_dv.length()-1; j++) |
|
589 ntot *= new_dv(j); |
|
590 |
|
591 for (octave_idx_type i = 0; i < nel; i++) |
|
592 { |
|
593 octave_idx_type n = ntot; |
|
594 octave_idx_type ii = i; |
|
595 for (int j = new_dv.length() - 1; j >= 0 ; j--) |
|
596 { |
|
597 if (! idx_colon(j)) |
|
598 lst (j) = ii/n + 1; |
|
599 ii = ii % n; |
|
600 if (j != 0) |
|
601 n /= new_dv(j-1); |
|
602 } |
|
603 ret(i) = args(0).do_index_op(lst, 0); |
|
604 } |
|
605 |
|
606 retval = ret; |
|
607 } |
|
608 } |
|
609 |
|
610 return retval; |
|
611 } |
|
612 |
5988
|
613 /* |
|
614 |
|
615 %!assert(num2cell([1,2;3,4]),{1,2;3,4}) |
|
616 %!assert(num2cell([1,2;3,4],1),{[1;3],[2;4]}) |
|
617 %!assert(num2cell([1,2;3,4],2),{[1,2];[3,4]}) |
|
618 |
|
619 */ |
|
620 |
5784
|
621 DEFUN_DLD (mat2cell, args, , |
|
622 "-*- texinfo -*-\n\ |
|
623 @deftypefn {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{m}, @var{n})\n\ |
|
624 @deftypefnx {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{d1}, @var{d2}, @dots{})\n\ |
|
625 @deftypefnx {Loadable Function} {@var{b} =} mat2cell (@var{a}, @var{r})\n\ |
|
626 Converts the matrix @var{a} to a cell array If @var{a} is 2-D, then\n\ |
|
627 it is required that @code{sum (@var{m}) == size (@var{a}, 1)} and\n\ |
|
628 @code{sum (@var{n}) == size (@var{a}, 2)}. Similarly, if @var{a} is\n\ |
|
629 a multi-dimensional and the number of dimensional arguments is equal\n\ |
|
630 to the dimensions of @var{a}, then it is required that @code{sum (@var{di})\n\ |
|
631 == size (@var{a}, i)}.\n\ |
|
632 \n\ |
|
633 Given a single dimensional argument @var{r}, the other dimensional\n\ |
|
634 arguments are assumed to equal @code{size (@var{a},@var{i})}.\n\ |
|
635 \n\ |
|
636 An example of the use of mat2cell is\n\ |
|
637 \n\ |
|
638 @example\n\ |
|
639 @group\n\ |
|
640 mat2cell (reshape(1:16,4,4),[3,1],[3,1])\n\ |
|
641 @result{} @{\n\ |
|
642 [1,1] =\n\ |
|
643 \n\ |
|
644 1 5 9\n\ |
|
645 2 6 10\n\ |
|
646 3 7 11\n\ |
|
647 \n\ |
|
648 [2,1] =\n\ |
|
649 \n\ |
|
650 4 8 12\n\ |
|
651 \n\ |
|
652 [1,2] =\n\ |
|
653 \n\ |
|
654 13\n\ |
|
655 14\n\ |
|
656 15\n\ |
|
657 \n\ |
|
658 [2,2] = 16\n\ |
|
659 @}\n\ |
|
660 @end group\n\ |
|
661 @end example\n\ |
5829
|
662 @seealso{num2cell, cell2mat}\n\ |
5784
|
663 @end deftypefn") |
|
664 { |
|
665 int nargin = args.length(); |
|
666 octave_value retval; |
|
667 |
|
668 if (nargin < 2) |
|
669 usage ("mat2cell"); |
|
670 else |
|
671 { |
|
672 dim_vector dv = args(0).dims(); |
|
673 dim_vector new_dv; |
|
674 new_dv.resize(dv.length()); |
|
675 |
|
676 if (nargin > 2) |
|
677 { |
|
678 octave_idx_type nmax = -1; |
|
679 |
|
680 if (nargin - 1 != dv.length()) |
|
681 error ("mat2cell: Incorrect number of dimensions"); |
|
682 else |
|
683 { |
|
684 for (octave_idx_type j = 0; j < dv.length(); j++) |
|
685 { |
|
686 ColumnVector d = ColumnVector (args(j+1).vector_value |
|
687 (false, true)); |
|
688 |
|
689 if (d.length() < 1) |
|
690 { |
|
691 error ("mat2cell: dimension can not be empty"); |
|
692 break; |
|
693 } |
|
694 else |
|
695 { |
|
696 if (nmax < d.length()) |
|
697 nmax = d.length(); |
|
698 |
|
699 for (octave_idx_type i = 1; i < d.length(); i++) |
|
700 { |
|
701 OCTAVE_QUIT; |
|
702 |
|
703 if (d(i) >= 0) |
|
704 d(i) += d(i-1); |
|
705 else |
|
706 { |
|
707 error ("mat2cell: invalid dimensional argument"); |
|
708 break; |
|
709 } |
|
710 } |
|
711 |
|
712 if (d(0) < 0) |
|
713 error ("mat2cell: invalid dimensional argument"); |
|
714 |
|
715 if (d(d.length() - 1) != dv(j)) |
|
716 error ("mat2cell: inconsistent dimensions"); |
|
717 |
|
718 if (error_state) |
|
719 break; |
|
720 |
|
721 new_dv(j) = d.length(); |
|
722 } |
|
723 } |
|
724 } |
|
725 |
|
726 if (! error_state) |
|
727 { |
|
728 // Construct a matrix with the index values |
|
729 Matrix dimargs(nmax, new_dv.length()); |
|
730 for (octave_idx_type j = 0; j < new_dv.length(); j++) |
|
731 { |
|
732 OCTAVE_QUIT; |
|
733 |
|
734 ColumnVector d = ColumnVector (args(j+1).vector_value |
|
735 (false, true)); |
|
736 |
|
737 dimargs(0,j) = d(0); |
|
738 for (octave_idx_type i = 1; i < d.length(); i++) |
|
739 dimargs(i,j) = dimargs(i-1,j) + d(i); |
|
740 } |
|
741 |
|
742 |
|
743 octave_value_list lst (new_dv.length(), octave_value()); |
|
744 Cell ret (new_dv); |
|
745 octave_idx_type nel = new_dv.numel(); |
|
746 octave_idx_type ntot = 1; |
|
747 |
|
748 for (int j = 0; j < new_dv.length()-1; j++) |
|
749 ntot *= new_dv(j); |
|
750 |
|
751 for (octave_idx_type i = 0; i < nel; i++) |
|
752 { |
|
753 octave_idx_type n = ntot; |
|
754 octave_idx_type ii = i; |
|
755 for (octave_idx_type j = new_dv.length() - 1; j >= 0; j--) |
|
756 { |
|
757 OCTAVE_QUIT; |
|
758 |
|
759 octave_idx_type idx = ii / n; |
|
760 lst (j) = Range((idx == 0 ? 1. : dimargs(idx-1,j)+1.), |
|
761 dimargs(idx,j)); |
|
762 ii = ii % n; |
|
763 if (j != 0) |
|
764 n /= new_dv(j-1); |
|
765 } |
|
766 ret(i) = args(0).do_index_op(lst, 0); |
|
767 if (error_state) |
|
768 break; |
|
769 } |
|
770 |
|
771 if (!error_state) |
|
772 retval = ret; |
|
773 } |
|
774 } |
|
775 else |
|
776 { |
|
777 ColumnVector d = ColumnVector (args(1).vector_value |
|
778 (false, true)); |
|
779 |
|
780 double sumd = 0.; |
|
781 for (octave_idx_type i = 0; i < d.length(); i++) |
|
782 { |
|
783 OCTAVE_QUIT; |
|
784 |
|
785 if (d(i) >= 0) |
|
786 sumd += d(i); |
|
787 else |
|
788 { |
|
789 error ("mat2cell: invalid dimensional argument"); |
|
790 break; |
|
791 } |
|
792 } |
|
793 |
|
794 if (sumd != dv(0)) |
|
795 error ("mat2cell: inconsistent dimensions"); |
|
796 |
|
797 new_dv(0) = d.length(); |
|
798 for (octave_idx_type i = 1; i < dv.length(); i++) |
|
799 new_dv(i) = 1; |
|
800 |
|
801 if (! error_state) |
|
802 { |
|
803 octave_value_list lst (new_dv.length(), octave_value()); |
|
804 Cell ret (new_dv); |
|
805 |
|
806 for (octave_idx_type i = 1; i < new_dv.length(); i++) |
|
807 lst (i) = Range (1., static_cast<double>(dv(i))); |
|
808 |
|
809 double idx = 0.; |
|
810 for (octave_idx_type i = 0; i < new_dv(0); i++) |
|
811 { |
|
812 OCTAVE_QUIT; |
|
813 |
|
814 lst(0) = Range(idx + 1., idx + d(i)); |
|
815 ret(i) = args(0).do_index_op(lst, 0); |
|
816 idx += d(i); |
|
817 if (error_state) |
|
818 break; |
|
819 } |
|
820 |
|
821 if (!error_state) |
|
822 retval = ret; |
|
823 } |
|
824 } |
|
825 } |
|
826 |
|
827 return retval; |
|
828 } |
|
829 |
|
830 /* |
|
831 |
|
832 %!test |
|
833 %! x = reshape(1:20,5,4); |
|
834 %! c = mat2cell(x,[3,2],[3,1]); |
|
835 %! assert(c,{[1,6,11;2,7,12;3,8,13],[16;17;18];[4,9,14;5,10,15],[19;20]}) |
|
836 |
|
837 %!test |
|
838 %! x = 'abcdefghij'; |
|
839 %! c = mat2cell(x,1,[0,4,2,0,4,0]); |
|
840 %! empty1by0str = resize('',1,0); |
|
841 %! assert(c,{empty1by0str,'abcd','ef',empty1by0str,'ghij',empty1by0str}) |
|
842 |
|
843 */ |
|
844 |
5205
|
845 /* |
|
846 ;;; Local Variables: *** |
|
847 ;;; mode: C++ *** |
|
848 ;;; End: *** |
|
849 */ |