annotate src/DLD-FUNCTIONS/cellfun.cc @ 13229:983720941822

maint: improve readability of cellfun * cellfun.cc (try_cellfun_internal_ops, get_cellfun_options): New functions. (Fcellfun): Use them to improve readability.
author John W. Eaton <jwe@octave.org>
date Sun, 25 Sep 2011 22:33:47 -0400
parents 5a5cb2a4b71d
children c73cc5b67feb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
1 /*
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
2
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11457
diff changeset
3 Copyright (C) 2005-2011 Mohamed Kamoun
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
4 Copyright (C) 2009 Jaroslav Hajek
10521
4d1fc073fbb7 add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents: 10428
diff changeset
5 Copyright (C) 2010 VZLU Prague
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
6
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
7 This file is part of Octave.
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
8
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
9 Octave is free software; you can redistribute it and/or modify it
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
10 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
11 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
12 option) any later version.
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
13
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
14 Octave is distributed in the hope that it will be useful, but WITHOUT
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
17 for more details.
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
18
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
20 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
21 <http://www.gnu.org/licenses/>.
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
22
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
23 */
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
24
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
26 #include <config.h>
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
27 #endif
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
28
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
29 #include <string>
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
30 #include <vector>
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
31 #include <list>
8994
a8d30dc1beec cellfun optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8985
diff changeset
32 #include <memory>
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
33
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
34 #include "lo-mappers.h"
8377
25bc2d31e1bf improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents: 8373
diff changeset
35 #include "oct-locbuf.h"
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
36
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
37 #include "Cell.h"
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
38 #include "oct-map.h"
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
39 #include "defun-dld.h"
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
40 #include "parse.h"
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
41 #include "variables.h"
5451
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
42 #include "ov-colon.h"
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
43 #include "unwind-prot.h"
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
44 #include "gripes.h"
9728
70925b11ba46 again allow the obsolete syntax for cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9684
diff changeset
45 #include "utils.h"
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
46
10044
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
47 #include "ov-scalar.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
48 #include "ov-float.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
49 #include "ov-complex.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
50 #include "ov-flt-complex.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
51 #include "ov-bool.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
52 #include "ov-int8.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
53 #include "ov-int16.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
54 #include "ov-int32.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
55 #include "ov-int64.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
56 #include "ov-uint8.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
57 #include "ov-uint16.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
58 #include "ov-uint32.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
59 #include "ov-uint64.h"
2d7a8c227713 rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 10043
diff changeset
60
12952
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
61 #include "ov-fcn-handle.h"
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
62
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
63 static octave_value_list
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
64 get_output_list (octave_idx_type count, octave_idx_type nargout,
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
65 const octave_value_list& inputlist,
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
66 octave_value& func,
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
67 octave_value& error_handler)
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
68 {
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
69 octave_value_list tmp = func.do_multi_index_op (nargout, inputlist);
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
70
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
71 if (error_state)
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
72 {
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
73 if (error_handler.is_defined ())
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
74 {
11046
14993c9e857e amd.cc, cellfun.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents: 10964
diff changeset
75 octave_scalar_map msg;
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
76 msg.assign ("identifier", last_error_id ());
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
77 msg.assign ("message", last_error_message ());
11046
14993c9e857e amd.cc, cellfun.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents: 10964
diff changeset
78 msg.assign ("index", static_cast<double> (count + static_cast<octave_idx_type>(1)));
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
79
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
80 octave_value_list errlist = inputlist;
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
81 errlist.prepend (msg);
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
82
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
83 buffer_error_messages--;
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
84
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
85 error_state = 0;
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
86
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
87 tmp = error_handler.do_multi_index_op (nargout, errlist);
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
88
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
89 buffer_error_messages++;
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
90
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
91 if (error_state)
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
92 tmp.clear ();
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
93 }
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
94 else
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
95 tmp.clear ();
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
96 }
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
97
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
98 return tmp;
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
99 }
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
100
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
101 static octave_value_list
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
102 try_cellfun_internal_ops (const octave_value_list& args, int nargin)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
103 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
104 octave_value_list retval;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
105
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
106 std::string name = args(0).string_value ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
107
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
108 const Cell f_args = args(1).cell_value ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
109
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
110 octave_idx_type k = f_args.numel ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
111
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
112 if (name == "isempty")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
113 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
114 boolNDArray result (f_args.dims ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
115 for (octave_idx_type count = 0; count < k ; count++)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
116 result(count) = f_args.elem(count).is_empty ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
117 retval(0) = result;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
118 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
119 else if (name == "islogical")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
120 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
121 boolNDArray result (f_args.dims ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
122 for (octave_idx_type count= 0; count < k ; count++)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
123 result(count) = f_args.elem(count).is_bool_type ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
124 retval(0) = result;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
125 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
126 else if (name == "isreal")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
127 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
128 boolNDArray result (f_args.dims ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
129 for (octave_idx_type count= 0; count < k ; count++)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
130 result(count) = f_args.elem(count).is_real_type ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
131 retval(0) = result;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
132 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
133 else if (name == "length")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
134 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
135 NDArray result (f_args.dims ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
136 for (octave_idx_type count= 0; count < k ; count++)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
137 result(count) = static_cast<double> (f_args.elem(count).length ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
138 retval(0) = result;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
139 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
140 else if (name == "ndims")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
141 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
142 NDArray result (f_args.dims ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
143 for (octave_idx_type count = 0; count < k ; count++)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
144 result(count) = static_cast<double> (f_args.elem(count).ndims ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
145 retval(0) = result;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
146 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
147 else if (name == "prodofsize" || name == "numel")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
148 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
149 NDArray result (f_args.dims ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
150 for (octave_idx_type count = 0; count < k ; count++)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
151 result(count) = static_cast<double> (f_args.elem(count).numel ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
152 retval(0) = result;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
153 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
154 else if (name == "size")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
155 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
156 if (nargin == 3)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
157 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
158 int d = args(2).nint_value () - 1;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
159
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
160 if (d < 0)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
161 error ("cellfun: K must be a positive integer");
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
162
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
163 if (! error_state)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
164 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
165 NDArray result (f_args.dims ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
166 for (octave_idx_type count = 0; count < k ; count++)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
167 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
168 dim_vector dv = f_args.elem(count).dims ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
169 if (d < dv.length ())
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
170 result(count) = static_cast<double> (dv(d));
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
171 else
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
172 result(count) = 1.0;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
173 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
174 retval(0) = result;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
175 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
176 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
177 else
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
178 error ("cellfun: not enough arguments for \"size\"");
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
179 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
180 else if (name == "isclass")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
181 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
182 if (nargin == 3)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
183 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
184 std::string class_name = args(2).string_value();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
185 boolNDArray result (f_args.dims ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
186 for (octave_idx_type count = 0; count < k ; count++)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
187 result(count) = (f_args.elem(count).class_name() == class_name);
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
188
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
189 retval(0) = result;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
190 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
191 else
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
192 error ("cellfun: not enough arguments for \"isclass\"");
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
193 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
194
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
195 return retval;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
196 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
197
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
198 static void
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
199 get_cellfun_options (const octave_value_list& args, int& nargin,
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
200 bool& uniform_output, octave_value& error_handler)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
201 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
202 while (nargin > 3 && args(nargin-2).is_string ())
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
203 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
204 std::string arg = args(nargin-2).string_value ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
205
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
206 std::transform (arg.begin (), arg.end (),
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
207 arg.begin (), tolower);
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
208
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
209 if (arg == "uniformoutput")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
210 uniform_output = args(nargin-1).bool_value();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
211 else if (arg == "errorhandler")
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
212 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
213 if (args(nargin-1).is_function_handle ()
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
214 || args(nargin-1).is_inline_function ())
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
215 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
216 error_handler = args(nargin-1);
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
217 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
218 else if (args(nargin-1).is_string ())
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
219 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
220 std::string err_name = args(nargin-1).string_value ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
221
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
222 error_handler = symbol_table::find_function (err_name);
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
223
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
224 if (error_handler.is_undefined ())
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
225 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
226 error ("cellfun: invalid function NAME: %s",
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
227 err_name.c_str ());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
228 break;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
229 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
230 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
231 else
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
232 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
233 error ("cellfun: invalid value for 'ErrorHandler' function");
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
234 break;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
235 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
236 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
237 else
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
238 {
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
239 error ("cellfun: unrecognized parameter %s",
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
240 arg.c_str());
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
241 break;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
242 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
243
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
244 nargin -= 2;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
245 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
246
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
247 nargin -= 1;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
248 }
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
249
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
250 DEFUN_DLD (cellfun, args, nargout,
8715
954b6f69f51d doc fixes
John W. Eaton <jwe@octave.org>
parents: 8377
diff changeset
251 "-*- texinfo -*-\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
252 @deftypefn {Loadable Function} {} cellfun (@var{name}, @var{C})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
253 @deftypefnx {Loadable Function} {} cellfun (\"size\", @var{C}, @var{k})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
254 @deftypefnx {Loadable Function} {} cellfun (\"isclass\", @var{C}, @var{class})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
255 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{C})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
256 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{C}, @var{D})\n\
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
257 @deftypefnx {Loadable Function} {[@var{a}, @dots{}] =} cellfun (@dots{})\n\
6854
364765c17648 [project @ 2007-09-01 00:03:10 by dbateman]
dbateman
parents: 6704
diff changeset
258 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'ErrorHandler', @var{errfunc})\n\
364765c17648 [project @ 2007-09-01 00:03:10 by dbateman]
dbateman
parents: 6704
diff changeset
259 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'UniformOutput', @var{val})\n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
260 \n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
261 Evaluate the function named @var{name} on the elements of the cell array\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
262 @var{C}. Elements in @var{C} are passed on to the named function\n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
263 individually. The function @var{name} can be one of the functions\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
264 \n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
265 @table @code\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
266 @item isempty\n\
5543
4d52e637a72a [project @ 2005-11-21 16:13:47 by jwe]
jwe
parents: 5451
diff changeset
267 Return 1 for empty elements.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
268 \n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
269 @item islogical\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
270 Return 1 for logical elements.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
271 \n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
272 @item isreal\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
273 Return 1 for real elements.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
274 \n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
275 @item length\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
276 Return a vector of the lengths of cell elements.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
277 \n\
5543
4d52e637a72a [project @ 2005-11-21 16:13:47 by jwe]
jwe
parents: 5451
diff changeset
278 @item ndims\n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
279 Return the number of dimensions of each element.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
280 \n\
12932
f951ffdd0133 doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
281 @item numel\n\
f951ffdd0133 doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
282 @itemx prodofsize\n\
f951ffdd0133 doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
283 Return the number of elements contained within each cell element. The\n\
f951ffdd0133 doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
284 number is the product of the dimensions of the object at each cell element.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
285 \n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
286 @item size\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
287 Return the size along the @var{k}-th dimension.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
288 \n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
289 @item isclass\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
290 Return 1 for elements of @var{class}.\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
291 @end table\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
292 \n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
293 Additionally, @code{cellfun} accepts an arbitrary function @var{func}\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
294 in the form of an inline function, function handle, or the name of a\n\
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
295 function (in a character string). In the case of a character string\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
296 argument, the function must accept a single argument named @var{x}, and\n\
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
297 it must return a string value. The function can take one or more arguments,\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
298 with the inputs arguments given by @var{C}, @var{D}, etc. Equally the\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
299 function can return one or more output arguments. For example:\n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
300 \n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
301 @example\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
302 @group\n\
12932
f951ffdd0133 doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
303 cellfun (\"atan2\", @{1, 0@}, @{0, 1@})\n\
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
304 @result{}ans = [1.57080 0.00000]\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
305 @end group\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
306 @end example\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
307 \n\
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
308 The number of output arguments of @code{cellfun} matches the number of output\n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
309 arguments of the function. The outputs of the function will be collected\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
310 into the output arguments of @code{cellfun} like this:\n\
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
311 \n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
312 @example\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
313 @group\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
314 function [a, b] = twoouts (x)\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
315 a = x;\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
316 b = x*x;\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
317 endfunction\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
318 [aa, bb] = cellfun(@@twoouts, @{1, 2, 3@})\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
319 @result{}\n\
12642
f96b9b9f141b doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents: 12483
diff changeset
320 aa =\n\
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
321 1 2 3\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
322 bb =\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
323 1 4 9\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
324 @end group\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
325 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
326 \n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
327 Note that per default the output argument(s) are arrays of the same size as\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
328 the input arguments. Input arguments that are singleton (1x1) cells will be\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
329 automatically expanded to the size of the other arguments.\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
330 \n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
331 If the parameter 'UniformOutput' is set to true (the default), then the\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
332 function must return scalars which will be concatenated into the return\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
333 array(s). If 'UniformOutput' is false, the outputs are concatenated into a\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
334 cell array (or cell arrays). For example:\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
335 \n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
336 @example\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
337 @group\n\
12932
f951ffdd0133 doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
338 cellfun (\"tolower\", @{\"Foo\", \"Bar\", \"FooBar\"@},\n\
6623
545847da3b88 [project @ 2007-05-15 02:23:08 by jwe]
jwe
parents: 6550
diff changeset
339 \"UniformOutput\",false)\n\
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
340 @result{} ans = @{\"foo\", \"bar\", \"foobar\"@}\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
341 @end group\n\
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
342 @end example\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
343 \n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
344 Given the parameter 'ErrorHandler', then @var{errfunc} defines a function to\n\
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
345 call in case @var{func} generates an error. The form of the function is\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
346 \n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
347 @example\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
348 function [@dots{}] = errfunc (@var{s}, @dots{})\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
349 @end example\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
350 \n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
351 @noindent\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
352 where there is an additional input argument to @var{errfunc} relative to\n\
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
353 @var{func}, given by @var{s}. This is a structure with the elements\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
354 'identifier', 'message' and 'index', giving respectively the error\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
355 identifier, the error message, and the index into the input arguments\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
356 of the element that caused the error. For example:\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
357 \n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
358 @example\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
359 @group\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
360 function y = foo (s, x), y = NaN; endfunction\n\
12932
f951ffdd0133 doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
361 cellfun (\"factorial\", @{-1,2@}, 'ErrorHandler', @@foo)\n\
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
362 @result{} ans = [NaN 2]\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
363 @end group\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
364 @end example\n\
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
365 \n\
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10670
diff changeset
366 @seealso{arrayfun, structfun, spfun}\n\
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5544
diff changeset
367 @end deftypefn")
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
368 {
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
369 octave_value_list retval;
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
370 int nargin = args.length ();
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
371 int nargout1 = (nargout < 1 ? 1 : nargout);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
372
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
373 if (nargin < 2)
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
374 {
12483
7a5aacf65f81 Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents: 11590
diff changeset
375 error ("cellfun: function requires at least 2 arguments");
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5784
diff changeset
376 print_usage ();
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
377 return retval;
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
378 }
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
379
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
380 octave_value func = args(0);
12949
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
381 bool symbol_table_lookup = false;
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
382
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
383 if (! args(1).is_cell ())
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
384 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
385 error ("cellfun: C must be a cell array");
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
386
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
387 return retval;
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
388 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
389
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
390 if (func.is_string ())
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
391 {
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
392 retval = try_cellfun_internal_ops (args, nargin);
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
393
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
394 if (error_state || ! retval.empty ())
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
395 return retval;
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
396
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
397 // See if we can convert the string into a function.
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
398
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
399 std::string name = args(0).string_value ();
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
400
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
401 if (! valid_identifier (name))
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
402 {
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
403 std::string fcn_name = unique_symbol_name ("__cellfun_fcn_");
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
404 std::string fname = "function y = " + fcn_name + "(x) y = ";
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
405
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
406 octave_function *ptr_func
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
407 = extract_function (args(0), "cellfun", fcn_name,
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
408 fname, "; endfunction");
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
409
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
410 if (ptr_func && ! error_state)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
411 func = octave_value (ptr_func, true);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
412 }
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
413 else
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
414 {
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
415 func = symbol_table::find_function (name);
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
416
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
417 if (func.is_undefined ())
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
418 error ("cellfun: invalid function NAME: %s", name.c_str ());
9728
70925b11ba46 again allow the obsolete syntax for cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9684
diff changeset
419
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
420 symbol_table_lookup = true;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
421 }
12949
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
422
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
423 if (error_state || ! retval.empty ())
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
424 return retval;
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
425 }
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
426
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
427 if (func.is_function_handle () || func.is_inline_function ()
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
428 || func.is_function ())
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
429 {
12949
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
430 // The following is an optimisation because the symbol table can
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
431 // give a more specific function class, so this can result in
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
432 // fewer polymorphic function calls as the function gets called
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
433 // for each value of the array.
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
434
12949
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
435 if (! symbol_table_lookup )
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
436 {
12952
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
437 if (func.is_function_handle ())
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
438 {
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
439 octave_fcn_handle* f = func.fcn_handle_value ();
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
440
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
441 // Overloaded function handles need to check the type of
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
442 // the arguments for each element of the array, so they
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
443 // cannot be optimised this way.
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
444
12952
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
445 if (f -> is_overloaded ())
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
446 goto nevermind;
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
447 }
5a5cb2a4b71d cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12949
diff changeset
448 octave_value f = symbol_table::find_function (func.function_value ()
12949
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
449 -> name ());
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
450 if (f.is_defined ())
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
451 func = f;
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
452 }
d1db86336a49 cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12948
diff changeset
453
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
454 nevermind:
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
455
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
456 bool uniform_output = true;
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
457 octave_value error_handler;
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
458
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
459 get_cellfun_options (args, nargin, uniform_output, error_handler);
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
460
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
461 if (error_state)
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
462 return octave_value_list ();
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
463
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
464 // Extract cell arguments.
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
466 octave_value_list inputlist (nargin, octave_value ());
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
467
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
468 OCTAVE_LOCAL_BUFFER (Cell, inputs, nargin);
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
469 OCTAVE_LOCAL_BUFFER (bool, mask, nargin);
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
470
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
471 // This is to prevent copy-on-write.
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
472 const Cell *cinputs = inputs;
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
473
9684
c5ff5f858cfd omission from last patch
Jaroslav Hajek <highegg@gmail.com>
parents: 9683
diff changeset
474 octave_idx_type k = 1;
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
475
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
476 dim_vector fdims (1, 1);
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
477
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
478 for (int j = 0; j < nargin; j++)
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
479 {
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
480 if (! args(j+1).is_cell ())
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
481 {
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
482 error ("cellfun: arguments must be cells");
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 10044
diff changeset
483 return octave_value_list ();
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
484 }
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
485
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
486 inputs[j] = args(j+1).cell_value ();
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
487 mask[j] = inputs[j].numel () != 1;
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
488 if (! mask[j])
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
489 inputlist(j) = cinputs[j](0);
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
490 }
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
491
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
492 for (int j = 0; j < nargin; j++)
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
493 {
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
494 if (mask[j])
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
495 {
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
496 fdims = inputs[j].dims ();
9683
5b3b9dcfd59c fix crash in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
497 k = inputs[j].numel ();
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
498 for (int i = j+1; i < nargin; i++)
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
499 {
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
500 if (mask[i] && inputs[i].dims () != fdims)
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
501 {
11590
4ced6b90fffb style fixes for warning and error messages in source files
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
502 error ("cellfun: dimensions mismatch");
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 10044
diff changeset
503 return octave_value_list ();
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
504 }
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
505 }
9683
5b3b9dcfd59c fix crash in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9678
diff changeset
506 break;
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
507 }
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
508 }
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
509
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
510 unwind_protect frame;
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
511 frame.protect_var (buffer_error_messages);
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
512
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
513 if (error_handler.is_defined ())
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
514 buffer_error_messages++;
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
515
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
516 // Apply functions.
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
517
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
518 if (uniform_output)
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
519 {
10670
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
520 std::list<octave_value_list> idx_list (1);
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
521 idx_list.front ().resize (1);
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
522 std::string idx_type = "(";
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
523
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
524 OCTAVE_LOCAL_BUFFER (octave_value, retv, nargout1);
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
525
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
526 for (octave_idx_type count = 0; count < k ; count++)
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
527 {
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
528 for (int j = 0; j < nargin; j++)
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
529 {
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
530 if (mask[j])
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
531 inputlist.xelem (j) = cinputs[j](count);
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
532 }
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
533
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
534 const octave_value_list tmp
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
535 = get_output_list (count, nargout, inputlist, func,
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
536 error_handler);
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
537
9459
cb0b21f34abc avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9450
diff changeset
538 if (error_state)
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
539 return retval;
9459
cb0b21f34abc avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9450
diff changeset
540
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
541 if (tmp.length () < nargout1)
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
542 {
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
543 if (tmp.length () < nargout)
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
544 {
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
545 error ("cellfun: too many output arguments");
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 10044
diff changeset
546 return octave_value_list ();
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
547 }
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
548 else
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
549 nargout1 = 0;
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
550 }
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
551
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
552 if (count == 0)
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
553 {
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
554 for (int j = 0; j < nargout1; j++)
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
555 {
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
556 octave_value val = tmp(j);
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
557
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
558 if (val.numel () == 1)
10670
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
559 retv[j] = val.resize (fdims);
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
560 else
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
561 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
562 error ("cellfun: all values must be scalars when UniformOutput = true");
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
563 break;
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
564 }
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
565 }
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
566 }
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
567 else
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
568 {
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
569 for (int j = 0; j < nargout1; j++)
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
570 {
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
571 octave_value val = tmp(j);
5989
e049385342f6 [project @ 2006-09-14 02:10:46 by jwe]
jwe
parents: 5988
diff changeset
572
10670
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
573 if (! retv[j].fast_elem_insert (count, val))
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
574 {
10670
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
575 if (val.numel () == 1)
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
576 {
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
577 idx_list.front ()(0) = count + 1.0;
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
578 retv[j].assign (octave_value::op_asn_eq,
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
579 idx_type, idx_list, val);
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
580
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
581 if (error_state)
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
582 break;
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
583 }
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
584 else
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
585 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
586 error ("cellfun: all values must be scalars when UniformOutput = true");
10670
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
587 break;
654fbde5dceb make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents: 10521
diff changeset
588 }
8994
a8d30dc1beec cellfun optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8985
diff changeset
589 }
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
590 }
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
591 }
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
592
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
593 if (error_state)
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
594 break;
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
595 }
8994
a8d30dc1beec cellfun optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8985
diff changeset
596
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
597 retval.resize (nargout1);
13229
983720941822 maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents: 12952
diff changeset
598
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
599 for (int j = 0; j < nargout1; j++)
11457
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
600 {
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
601 if (nargout > 0 && retv[j].is_undefined ())
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
602 retval(j) = NDArray (fdims);
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
603 else
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
604 retval(j) = retv[j];
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
605 }
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
606 }
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
607 else
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
608 {
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
609 OCTAVE_LOCAL_BUFFER (Cell, results, nargout1);
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
610 for (int j = 0; j < nargout1; j++)
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
611 results[j].resize (fdims);
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
612
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
613 for (octave_idx_type count = 0; count < k ; count++)
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
614 {
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
615 for (int j = 0; j < nargin; j++)
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
616 {
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
617 if (mask[j])
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
618 inputlist.xelem (j) = cinputs[j](count);
9465
40de4692c860 auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9459
diff changeset
619 }
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
620
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
621 const octave_value_list tmp = get_output_list (count, nargout, inputlist,
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
622 func, error_handler);
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
623
9459
cb0b21f34abc avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9450
diff changeset
624 if (error_state)
10181
a668fbd32e34 improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
625 return retval;
9459
cb0b21f34abc avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9450
diff changeset
626
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
627 if (tmp.length () < nargout1)
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
628 {
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
629 if (tmp.length () < nargout)
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
630 {
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
631 error ("cellfun: too many output arguments");
10066
2cd940306a06 make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents: 10044
diff changeset
632 return octave_value_list ();
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
633 }
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
634 else
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
635 nargout1 = 0;
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
636 }
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
637
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
638
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
639 for (int j = 0; j < nargout1; j++)
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
640 results[j](count) = tmp(j);
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
641 }
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
642
9988
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
643 retval.resize(nargout1);
76cf4aec34e9 allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents: 9740
diff changeset
644 for (int j = 0; j < nargout1; j++)
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
645 retval(j) = results[j];
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
646 }
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
647 }
9450
cf714e75c656 implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
648 else
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
649 error ("cellfun: argument NAME must be a string or function handle");
5205
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
650
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
651 return retval;
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
652 }
59592dcbb5d8 [project @ 2005-03-15 16:04:56 by jwe]
jwe
parents:
diff changeset
653
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
654 /*
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
655
11457
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
656 %!test
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
657 %! [a,b] = cellfun (@(x) x, cell (2, 0));
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
658 %! assert (a, zeros (2, 0));
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
659 %! assert (b, zeros (2, 0));
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
660
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
661 %!test
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
662 %! [a,b] = cellfun (@(x) x, cell (2, 0), "uniformoutput", false);
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
663 %! assert (a, cell (2, 0));
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
664 %! assert (b, cell (2, 0));
33f6384d2b78 cellfun compatibility fix
John W. Eaton <jwe@octave.org>
parents: 11046
diff changeset
665
8373
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
666 %% Test function to check the "Errorhandler" option
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
667 %!function [z] = cellfunerror (S, varargin)
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
668 %! z = S;
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
669 %! endfunction
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
670
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
671 %% 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
672 %% a function_handle or an anonymous function
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
673 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
674 %! A = cellfun ("islogical", {true, 0.1, false, i*2});
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
675 %! assert (A, [true, false, true, false]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
676 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
677 %! A = cellfun (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
678 %! assert (A, [true, false, true, false]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
679 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
680 %! A = cellfun (@islogical, {true, 0.1, false, i*2});
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
681 %! assert (A, [true, false, true, false]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
682 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
683 %! 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
684 %! assert (A, [true, false, true, false]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
685
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
686 %% 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
687 %% "isempty", "islogical", "length", "ndims" or "prodofsize"
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
688 %!test
10291
fc879f361bda make isreal matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10181
diff changeset
689 %! A = cellfun ("isreal", {true, 0.1, {}, i*2, [], "abc"});
fc879f361bda make isreal matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents: 10181
diff changeset
690 %! assert (A, [true, true, false, false, true, true]);
8373
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
691 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
692 %! 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
693 %! assert (A, [false, false, false, false, true, false]);
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 = 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
696 %! assert (A, [true, false, true, false, false, false]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
697 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
698 %! 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
699 %! assert (A, [1, 1, 1, 1, 0, 3]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
700 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
701 %! A = 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
702 %! assert (A, [2; 4]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
703 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
704 %! 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
705 %! assert (A, [4, 24]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
706
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
707 %% 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
708 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
709 %! 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
710 %! assert (A, [6, 7, 8]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
711 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
712 %! A = cellfun (@(x,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
713 %! "UniformOutput", false);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
714 %! assert (A, {6, 7, 8});
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
715 %!test %% Two input arguments of different types
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
716 %! 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
717 %! assert (A, [true, false]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
718 %!test %% Pass another variable to the anonymous function
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
719 %! 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
720 %! assert (A, [true, false]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
721 %!test %% Three ouptut arguments of different type
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
722 %! [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
723 %! assert (isequal (A, {true, true; [], true}));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
724 %! assert (isequal (B, {true, true; [], true}));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
725 %! assert (isequal (C, {10, 11; [], 12}));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
726
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
727 %% 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
728 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
729 %! 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
730 %! assert (A, [false, true]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
731 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
732 %! 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
733 %! "UniformOutput", true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
734 %! assert (A, [false; true]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
735 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
736 %! 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
737 %! assert (A, {false, true; false, true});
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
738 %!test %% Three ouptut arguments of same type
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
739 %! [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
740 %! "UniformOutput", false);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
741 %! assert (isequal (A, {true, []; [], true}));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
742 %! assert (isequal (B, {true, []; [], true}));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
743 %! assert (isequal (C, {true, []; [], true}));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
744 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
745 %! 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
746 %! "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
747 %! assert (isfield (A, "identifier"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
748 %! assert (isfield (A, "message"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
749 %! assert (isfield (A, "index"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
750 %! assert (isempty (A.message), false);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
751 %! assert (A.index, 1);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
752 %!test %% Overwriting setting of "UniformOutput" true
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
753 %! 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
754 %! "UniformOutput", true, "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
755 %! assert (isfield (A, "identifier"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
756 %! assert (isfield (A, "message"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
757 %! assert (isfield (A, "index"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
758 %! assert (isempty (A.message), false);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
759 %! assert (A.index, 1);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
760
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
761 %% 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
762 %!test
9622
40cbcdf12b54 fix broken tests
Jaroslav Hajek <highegg@gmail.com>
parents: 9465
diff changeset
763 %! A = cellfun (@(x,y) x>y, {1.1, 4.2}, {3.1, 2+3*i});
8373
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
764 %! assert (A, [false, true]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
765 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
766 %! 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
767 %! "UniformOutput", true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
768 %! assert (A, [false, true; false, false]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
769 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
770 %! 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
771 %! 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
772 %! assert (isequal (A{2}, [4, 5, 6]));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
773 %!test %% Three ouptut arguments of different type
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
774 %! [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
775 %! assert (isequal (A, {true, true; [], true}));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
776 %! assert (isequal (B, {true, true; [], true}));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
777 %! assert (isequal (C, {10, 11; [], 12}));
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
778 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
779 %! 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
780 %! "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
781 %! 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
782 %! 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
783 %! 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
784 %! 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
785 %! 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
786 %! 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
787 %!test %% Overwriting setting of "UniformOutput" true
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
788 %! 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
789 %! "UniformOutput", true, "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
790 %! 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
791 %! 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
792 %! 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
793 %! 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
794 %! 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
795 %! 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
796
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
797 %% 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
798 %!error %% "UniformOutput" false should be used
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
799 %! 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
800 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
801 %! 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
802 %! assert (A, [false; true]);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
803 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
804 %! 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
805 %! assert (A, {"abc", "def"});
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
806 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
807 %! 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
808 %! "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
809 %! 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
810 %! 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
811 %! 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
812 %! 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
813 %! 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
814 %!test %% Overwriting setting of "UniformOutput" true
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
815 %! 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
816 %! "UniformOutput", true, "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
817 %! 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
818 %! 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
819 %! 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
820 %! 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
821 %! 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
822
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
823 %% Structures cannot be handled by cellfun
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
824 %!error
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
825 %! 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
826 %! 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
827
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
828 %% 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
829 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
830 %! 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
831 %! assert (A, [1, 0], 1e-16);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
832 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
833 %! 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
834 %! "UniformOutput", true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
835 %! assert (A, [1; 0], 1e-16);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
836 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
837 %! 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
838 %! "UniformOutput", false);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
839 %! assert (A, {true, false});
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
840 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
841 %! 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
842 %! "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
843 %! 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
844 %! 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
845 %! 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
846 %! 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
847 %! 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
848 %!test %% Overwriting setting of "UniformOutput" true
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
849 %! 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
850 %! "UniformOutput", true, "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
851 %! 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
852 %! 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
853 %! 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
854 %! 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
855 %! 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
856
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
857 %% 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
858 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
859 %! 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
860 %! 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
861 %! assert (A, true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
862 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
863 %! 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
864 %! 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
865 %! "UniformOutput", true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
866 %! assert (A, true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
867 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
868 %! 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
869 %! 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
870 %! "UniformOutput", false);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
871 %! assert (A, {true});
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
872 %!test
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
873 %! 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
874 %! 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
875 %! "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
876 %! assert (isfield (A, "identifier"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
877 %! assert (isfield (A, "message"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
878 %! assert (isfield (A, "index"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
879 %! assert (isempty (A.message), false);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
880 %! assert (A.index, 1);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
881 %!test %% Overwriting setting of "UniformOutput" true
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
882 %! 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
883 %! 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
884 %! "UniformOutput", true, "ErrorHandler", @cellfunerror);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
885 %! assert (isfield (A, "identifier"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
886 %! assert (isfield (A, "message"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
887 %! assert (isfield (A, "index"), true);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
888 %! assert (isempty (A.message), false);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
889 %! assert (A.index, 1);
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
890
63fe023d7898 Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents: 8347
diff changeset
891 %% A lot of other tests
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
892 %!error(cellfun(1))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
893 %!error(cellfun('isclass',1))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
894 %!error(cellfun('size',1))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
895 %!error(cellfun(@sin,{[]},'BadParam',false))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
896 %!error(cellfun(@sin,{[]},'UniformOuput'))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
897 %!error(cellfun(@sin,{[]},'ErrorHandler'))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
898 %!assert(cellfun(@sin,{0,1}),sin([0,1]))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
899 %!assert(cellfun(inline('sin(x)'),{0,1}),sin([0,1]))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
900 %!assert(cellfun('sin',{0,1}),sin([0,1]))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
901 %!assert(cellfun('isempty',{1,[]}),[false,true])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
902 %!assert(cellfun('islogical',{false,pi}),[true,false])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
903 %!assert(cellfun('isreal',{1i,1}),[false,true])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
904 %!assert(cellfun('length',{zeros(2,2),1}),[2,1])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
905 %!assert(cellfun('prodofsize',{zeros(2,2),1}),[4,1])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
906 %!assert(cellfun('ndims',{zeros([2,2,2]),1}),[3,2])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
907 %!assert(cellfun('isclass',{zeros([2,2,2]),'test'},'double'),[true,false])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
908 %!assert(cellfun('size',{zeros([1,2,3]),1},1),[1,1])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
909 %!assert(cellfun('size',{zeros([1,2,3]),1},2),[2,1])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
910 %!assert(cellfun('size',{zeros([1,2,3]),1},3),[3,1])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
911 %!assert(cellfun(@atan2,{1,1},{1,2}),[atan2(1,1),atan2(1,2)])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
912 %!assert(cellfun(@atan2,{1,1},{1,2},'UniformOutput',false),{atan2(1,1),atan2(1,2)})
6422
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents: 5989
diff changeset
913 %!assert(cellfun(@sin,{1,2;3,4}),sin([1,2;3,4]))
9b982dd07654 [project @ 2007-03-20 21:26:12 by dbateman]
dbateman
parents: 5989
diff changeset
914 %!assert(cellfun(@atan2,{1,1;1,1},{1,2;1,2}),atan2([1,1;1,1],[1,2;1,2]))
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
915 %!error(cellfun(@factorial,{-1,3}))
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
916 %!assert(cellfun(@factorial,{-1,3},'ErrorHandler',@(x,y) NaN),[NaN,6])
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
917 %!test
6704
781777998927 [project @ 2007-06-13 05:23:56 by jwe]
jwe
parents: 6623
diff changeset
918 %! [a,b,c]=cellfun(@fileparts,{fullfile("a","b","c.d"),fullfile("e","f","g.h")},'UniformOutput',false);
781777998927 [project @ 2007-06-13 05:23:56 by jwe]
jwe
parents: 6623
diff changeset
919 %! assert(a,{fullfile("a","b"),fullfile("e","f")})
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
920 %! assert(b,{'c','g'})
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
921 %! assert(c,{'.d','.h'})
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
922
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
923 */
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
924
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
925 static void
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
926 do_num2cell_helper (const dim_vector& dv,
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
927 const Array<int>& dimv,
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
928 dim_vector& celldv, dim_vector& arraydv,
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
929 Array<int>& perm)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
930 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
931 int dvl = dimv.length ();
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
932 int maxd = dv.length ();
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
933 celldv = dv;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
934 for (int i = 0; i < dvl; i++)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
935 maxd = std::max (maxd, dimv(i));
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
936 if (maxd > dv.length ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
937 celldv.resize (maxd, 1);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
938 arraydv = celldv;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
939
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
940 OCTAVE_LOCAL_BUFFER_INIT (bool, sing, maxd, false);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
941
10350
12884915a8e4 merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents: 10291
diff changeset
942 perm.clear (maxd, 1);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
943 for (int i = 0; i < dvl; i++)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
944 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
945 int k = dimv(i) - 1;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
946 if (k < 0)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
947 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
948 error ("num2cell: dimension indices must be positive");
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
949 return;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
950 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
951 else if (i > 0 && k < dimv(i-1) - 1)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
952 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
953 error ("num2cell: dimension indices must be strictly increasing");
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
954 return;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
955 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
956
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
957 sing[k] = true;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
958 perm(i) = k;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
959 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
960
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
961 for (int k = 0, i = dvl; k < maxd; k++)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
962 if (! sing[k])
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
963 perm(i++) = k;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
964
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
965 for (int i = 0; i < maxd; i++)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
966 if (sing[i])
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
967 celldv(i) = 1;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
968 else
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
969 arraydv(i) = 1;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
970 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
971
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
972 template<class NDA>
10756
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
973 static inline typename NDA::element_type
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
974 do_num2cell_elem (const NDA& array, octave_idx_type i)
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
975 { return array(i); }
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
976
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
977 static inline Cell
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
978 do_num2cell_elem (const Cell& array, octave_idx_type i)
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
979 { return Cell (array(i)); }
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
980
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
981
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
982 template<class NDA>
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
983 static Cell
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
984 do_num2cell (const NDA& array, const Array<int>& dimv)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
985 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
986 if (dimv.is_empty ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
987 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
988 Cell retval (array.dims ());
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
989 octave_idx_type nel = array.numel ();
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
990 for (octave_idx_type i = 0; i < nel; i++)
10756
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
991 retval.xelem (i) = do_num2cell_elem (array, i);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
992
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
993 return retval;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
994 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
995 else
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
996 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
997 dim_vector celldv, arraydv;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
998 Array<int> perm;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
999 do_num2cell_helper (array.dims (), dimv, celldv, arraydv, perm);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1000 if (error_state)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1001 return Cell ();
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1002
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1003 NDA parray = array.permute (perm);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1004
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1005 octave_idx_type nela = arraydv.numel (), nelc = celldv.numel ();
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1006 parray = parray.reshape (dim_vector (nela, nelc));
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1007
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1008 Cell retval (celldv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1009 for (octave_idx_type i = 0; i < nelc; i++)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1010 {
9731
7b9cbaad68d6 extend Array<T>
Jaroslav Hajek <highegg@gmail.com>
parents: 9728
diff changeset
1011 retval.xelem (i) = NDA (parray.column (i).reshape (arraydv));
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1012 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1013
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1014 return retval;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1015 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1016 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1017
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1018
5451
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1019 DEFUN_DLD (num2cell, args, ,
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1020 "-*- texinfo -*-\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1021 @deftypefn {Loadable Function} {@var{C} =} num2cell (@var{A})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1022 @deftypefnx {Loadable Function} {@var{C} =} num2cell (@var{A}, @var{dim})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1023 Convert the numeric matrix @var{A} to a cell array. If @var{dim} is\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1024 defined, the value @var{C} is of dimension 1 in this dimension and the\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1025 elements of @var{A} are placed into @var{C} in slices. For example:\n\
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1026 \n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1027 @example\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1028 @group\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1029 num2cell([1,2;3,4])\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1030 @result{} ans =\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1031 @{\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1032 [1,1] = 1\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1033 [2,1] = 3\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1034 [1,2] = 2\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1035 [2,2] = 4\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1036 @}\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1037 num2cell([1,2;3,4],1)\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1038 @result{} ans =\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1039 @{\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1040 [1,1] =\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1041 1\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1042 3\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1043 [1,2] =\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1044 2\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1045 4\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1046 @}\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1047 @end group\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1048 @end example\n\
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1049 \n\
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5544
diff changeset
1050 @seealso{mat2cell}\n\
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1051 @end deftypefn")
5451
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1052 {
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1053 int nargin = args.length();
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1054 octave_value retval;
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1055
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1056 if (nargin < 1 || nargin > 2)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5784
diff changeset
1057 print_usage ();
5451
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1058 else
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1059 {
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1060 octave_value array = args(0);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1061 Array<int> dimv;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1062 if (nargin > 1)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1063 dimv = args (1).int_vector_value (true);
5451
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1064
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1065 if (error_state)
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1066 ;
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1067 else if (array.is_bool_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1068 retval = do_num2cell (array.bool_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1069 else if (array.is_char_matrix ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1070 retval = do_num2cell (array.char_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1071 else if (array.is_numeric_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1072 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1073 if (array.is_integer_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1074 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1075 if (array.is_int8_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1076 retval = do_num2cell (array.int8_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1077 else if (array.is_int16_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1078 retval = do_num2cell (array.int16_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1079 else if (array.is_int32_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1080 retval = do_num2cell (array.int32_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1081 else if (array.is_int64_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1082 retval = do_num2cell (array.int64_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1083 else if (array.is_uint8_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1084 retval = do_num2cell (array.uint8_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1085 else if (array.is_uint16_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1086 retval = do_num2cell (array.uint16_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1087 else if (array.is_uint32_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1088 retval = do_num2cell (array.uint32_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1089 else if (array.is_uint64_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1090 retval = do_num2cell (array.uint64_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1091 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1092 else if (array.is_complex_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1093 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1094 if (array.is_single_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1095 retval = do_num2cell (array.float_complex_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1096 else
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1097 retval = do_num2cell (array.complex_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1098 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1099 else
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1100 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1101 if (array.is_single_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1102 retval = do_num2cell (array.float_array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1103 else
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1104 retval = do_num2cell (array.array_value (), dimv);
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1105 }
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1106 }
10756
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
1107 else if (array.is_map ())
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
1108 retval = do_num2cell (array.map_value (), dimv);
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
1109 else if (array.is_cell ())
d808eb829d48 optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents: 10711
diff changeset
1110 retval = do_num2cell (array.cell_value (), dimv);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1111 else
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1112 gripe_wrong_type_arg ("num2cell", array);
5451
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1113 }
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1114
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1115 return retval;
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1116 }
ed08548b9054 [project @ 2005-09-15 19:52:50 by jwe]
jwe
parents: 5307
diff changeset
1117
5988
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
1118 /*
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
1119
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
1120 %!assert(num2cell([1,2;3,4]),{1,2;3,4})
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
1121 %!assert(num2cell([1,2;3,4],1),{[1;3],[2;4]})
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
1122 %!assert(num2cell([1,2;3,4],2),{[1,2];[3,4]})
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
1123
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
1124 */
d9ce802628e6 [project @ 2006-09-12 21:31:41 by dbateman]
dbateman
parents: 5829
diff changeset
1125
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1126 static bool
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1127 mat2cell_mismatch (const dim_vector& dv,
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1128 const Array<octave_idx_type> *d, int nd)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1129 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1130 for (int i = 0; i < nd; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1131 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1132 octave_idx_type s = 0;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1133 for (octave_idx_type j = 0; j < d[i].length (); j++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1134 s += d[i](j);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1135
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1136 octave_idx_type r = i < dv.length () ? dv(i) : 1;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1137
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1138 if (s != r)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1139 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1140 error ("mat2cell: mismatch on %d-th dimension (%d != %d)",
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1141 i+1, r, s);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1142 return true;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1143 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1144 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1145
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1146 return false;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1147 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1148
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1149 template<class container>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1150 static void
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1151 prepare_idx (container *idx, int idim, int nd,
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1152 const Array<octave_idx_type>* d)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1153 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1154 octave_idx_type nidx = idim < nd ? d[idim].numel () : 1;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1155 if (nidx == 1)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1156 idx[0] = idx_vector::colon;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1157 else
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1158 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1159 octave_idx_type l = 0;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1160 for (octave_idx_type i = 0; i < nidx; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1161 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1162 octave_idx_type u = l + d[idim](i);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1163 idx[i] = idx_vector (l, u);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1164 l = u;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1165 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1166 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1167 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1168
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1169 // 2D specialization, works for Array, Sparse and octave_map.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1170 // Uses 1D or 2D indexing.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1171
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1172 template <class Array2D>
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1173 static Cell
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1174 do_mat2cell_2d (const Array2D& a, const Array<octave_idx_type> *d, int nd)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1175 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1176 NoAlias<Cell> retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1177 assert (nd == 1 || nd == 2);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1178 assert (a.ndims () == 2);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1179
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1180 if (mat2cell_mismatch (a.dims (), d, nd))
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1181 return retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1182
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1183 octave_idx_type nridx = d[0].length ();
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1184 octave_idx_type ncidx = nd == 1 ? 1 : d[1].length ();
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1185 retval.clear (nridx, ncidx);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1186
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1187 int ivec = -1;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1188 if (a.rows () > 1 && a.cols () == 1 && ncidx == 1)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1189 ivec = 0;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1190 else if (a.rows () == 1 && nridx == 1 && nd == 2)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1191 ivec = 1;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1192
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1193 if (ivec >= 0)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1194 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1195 // Vector split. Use 1D indexing.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1196 octave_idx_type l = 0, nidx = (ivec == 0 ? nridx : ncidx);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1197 for (octave_idx_type i = 0; i < nidx; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1198 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1199 octave_idx_type u = l + d[ivec](i);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1200 retval(i) = a.index (idx_vector (l, u));
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1201 l = u;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1202 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1203 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1204 else
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1205 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1206 // General 2D case. Use 2D indexing.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1207 OCTAVE_LOCAL_BUFFER (idx_vector, ridx, nridx);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1208 prepare_idx (ridx, 0, nd, d);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1209
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1210 OCTAVE_LOCAL_BUFFER (idx_vector, cidx, ncidx);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1211 prepare_idx (cidx, 1, nd, d);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1212
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1213 for (octave_idx_type j = 0; j < ncidx; j++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1214 for (octave_idx_type i = 0; i < nridx; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1215 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1216 octave_quit ();
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1217
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1218 retval(i,j) = a.index (ridx[i], cidx[j]);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1219 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1220 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1221
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1222 return retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1223 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1224
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1225 // Nd case. Works for Arrays and octave_map.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1226 // Uses Nd indexing.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1227
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1228 template <class ArrayND>
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1229 Cell
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1230 do_mat2cell_nd (const ArrayND& a, const Array<octave_idx_type> *d, int nd)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1231 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1232 NoAlias<Cell> retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1233 assert (nd >= 1);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1234
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1235 if (mat2cell_mismatch (a.dims (), d, nd))
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1236 return retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1237
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1238 dim_vector rdv = dim_vector::alloc (nd);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1239 OCTAVE_LOCAL_BUFFER (octave_idx_type, nidx, nd);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1240 octave_idx_type idxtot = 0;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1241 for (int i = 0; i < nd; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1242 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1243 rdv(i) = nidx[i] = d[i].length ();
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1244 idxtot += nidx[i];
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1245 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1246
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1247 retval.clear (rdv);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1248
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1249 OCTAVE_LOCAL_BUFFER (idx_vector, xidx, idxtot);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1250 OCTAVE_LOCAL_BUFFER (idx_vector *, idx, nd);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1251
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1252 idxtot = 0;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1253 for (int i = 0; i < nd; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1254 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1255 idx[i] = xidx + idxtot;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1256 prepare_idx (idx[i], i, nd, d);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1257 idxtot += nidx[i];
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1258 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1259
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1260 OCTAVE_LOCAL_BUFFER_INIT (octave_idx_type, ridx, nd, 0);
11570
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
1261 NoAlias< Array<idx_vector> > ra_idx
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
1262 (dim_vector (1, std::max (nd, a.ndims ())), idx_vector::colon);
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1263
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1264 for (octave_idx_type j = 0; j < retval.numel (); j++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1265 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1266 octave_quit ();
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1267
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1268 for (int i = 0; i < nd; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1269 ra_idx(i) = idx[i][ridx[i]];
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1270
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1271 retval(j) = a.index (ra_idx);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1272
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1273 rdv.increment_index (ridx);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1274 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1275
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1276 return retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1277 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1278
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1279 // Dispatcher.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1280 template <class ArrayND>
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1281 Cell
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1282 do_mat2cell (const ArrayND& a, const Array<octave_idx_type> *d, int nd)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1283 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1284 if (a.ndims () == 2 && nd <= 2)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1285 return do_mat2cell_2d (a, d, nd);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1286 else
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1287 return do_mat2cell_nd (a, d, nd);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1288 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1289
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1290 // General case. Works for any class supporting do_index_op.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1291 // Uses Nd indexing.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1292
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1293 Cell
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1294 do_mat2cell (octave_value& a, const Array<octave_idx_type> *d, int nd)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1295 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1296 NoAlias<Cell> retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1297 assert (nd >= 1);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1298
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1299 if (mat2cell_mismatch (a.dims (), d, nd))
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1300 return retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1301
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1302 dim_vector rdv = dim_vector::alloc (nd);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1303 OCTAVE_LOCAL_BUFFER (octave_idx_type, nidx, nd);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1304 octave_idx_type idxtot = 0;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1305 for (int i = 0; i < nd; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1306 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1307 rdv(i) = nidx[i] = d[i].length ();
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1308 idxtot += nidx[i];
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1309 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1310
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1311 retval.clear (rdv);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1312
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1313 OCTAVE_LOCAL_BUFFER (octave_value, xidx, idxtot);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1314 OCTAVE_LOCAL_BUFFER (octave_value *, idx, nd);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1315
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1316 idxtot = 0;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1317 for (int i = 0; i < nd; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1318 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1319 idx[i] = xidx + idxtot;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1320 prepare_idx (idx[i], i, nd, d);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1321 idxtot += nidx[i];
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1322 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1323
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1324 OCTAVE_LOCAL_BUFFER_INIT (octave_idx_type, ridx, nd, 0);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1325 octave_value_list ra_idx (std::max (nd, a.ndims ()),
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1326 octave_value::magic_colon_t);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1327
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1328 for (octave_idx_type j = 0; j < retval.numel (); j++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1329 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1330 octave_quit ();
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1331
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1332 for (int i = 0; i < nd; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1333 ra_idx(i) = idx[i][ridx[i]];
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1334
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1335 retval(j) = a.do_index_op (ra_idx);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1336
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1337 if (error_state)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1338 break;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1339
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1340 rdv.increment_index (ridx);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1341 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1342
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1343 return retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1344 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1345
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1346 DEFUN_DLD (mat2cell, args, ,
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1347 "-*- texinfo -*-\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1348 @deftypefn {Loadable Function} {@var{C} =} mat2cell (@var{A}, @var{m}, @var{n})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1349 @deftypefnx {Loadable Function} {@var{C} =} mat2cell (@var{A}, @var{d1}, @var{d2}, @dots{})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1350 @deftypefnx {Loadable Function} {@var{C} =} mat2cell (@var{A}, @var{r})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1351 Convert the matrix @var{A} to a cell array. If @var{A} is 2-D, then\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1352 it is required that @code{sum (@var{m}) == size (@var{A}, 1)} and\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1353 @code{sum (@var{n}) == size (@var{A}, 2)}. Similarly, if @var{A} is\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1354 multi-dimensional and the number of dimensional arguments is equal\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1355 to the dimensions of @var{A}, then it is required that @code{sum (@var{di})\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1356 == size (@var{A}, i)}.\n\
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1357 \n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1358 Given a single dimensional argument @var{r}, the other dimensional\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1359 arguments are assumed to equal @code{size (@var{A},@var{i})}.\n\
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1360 \n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1361 An example of the use of mat2cell is\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1362 \n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1363 @example\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1364 mat2cell (reshape(1:16,4,4),[3,1],[3,1])\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1365 @result{} @{\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1366 [1,1] =\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1367 \n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1368 1 5 9\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1369 2 6 10\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1370 3 7 11\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1371 \n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1372 [2,1] =\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1373 \n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1374 4 8 12\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1375 \n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1376 [1,2] =\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1377 \n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1378 13\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1379 14\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1380 15\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1381 \n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1382 [2,2] = 16\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1383 @}\n\
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1384 @end example\n\
5829
93785a1b0f97 [project @ 2006-05-23 18:34:10 by jwe]
jwe
parents: 5823
diff changeset
1385 @seealso{num2cell, cell2mat}\n\
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1386 @end deftypefn")
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1387 {
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1388 int nargin = args.length();
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1389 octave_value retval;
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1390
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1391 if (nargin < 2)
7038
4482ba9814b7 [project @ 2007-10-19 16:05:48 by jwe]
jwe
parents: 7017
diff changeset
1392 print_usage ();
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1393 else
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1394 {
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1395 // Prepare indices.
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1396 OCTAVE_LOCAL_BUFFER (Array<octave_idx_type>, d, nargin-1);
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1397
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1398 for (int i = 1; i < nargin; i++)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1399 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1400 d[i-1] = args(i).octave_idx_type_vector_value (true);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1401 if (error_state)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1402 return retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1403 }
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1404
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1405 octave_value a = args(0);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1406 bool sparse = a.is_sparse_type ();
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1407 if (sparse && nargin > 3)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1408 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1409 error ("mat2cell: sparse arguments only support 2D indexing");
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1410 return retval;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1411 }
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1412
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1413 switch (a.builtin_type ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10125
diff changeset
1414 {
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1415 case btyp_double:
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1416 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1417 if (sparse)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1418 retval = do_mat2cell_2d (a.sparse_matrix_value (), d, nargin-1);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1419 else
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1420 retval = do_mat2cell (a.array_value (), d, nargin - 1);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1421 break;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1422 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1423 case btyp_complex:
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1424 {
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1425 if (sparse)
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1426 retval = do_mat2cell_2d (a.sparse_complex_matrix_value (), d, nargin-1);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1427 else
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1428 retval = do_mat2cell (a.complex_array_value (), d, nargin - 1);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1429 break;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1430 }
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1431 #define BTYP_BRANCH(X,Y) \
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1432 case btyp_ ## X: \
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1433 retval = do_mat2cell (a.Y ## _value (), d, nargin - 1); \
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1434 break
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1435
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1436 BTYP_BRANCH (float, float_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1437 BTYP_BRANCH (float_complex, float_complex_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1438 BTYP_BRANCH (bool, bool_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1439 BTYP_BRANCH (char, char_array);
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1440
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1441 BTYP_BRANCH (int8, int8_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1442 BTYP_BRANCH (int16, int16_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1443 BTYP_BRANCH (int32, int32_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1444 BTYP_BRANCH (int64, int64_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1445 BTYP_BRANCH (uint8, uint8_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1446 BTYP_BRANCH (uint16, uint16_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1447 BTYP_BRANCH (uint32, uint32_array);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1448 BTYP_BRANCH (uint64, uint64_array);
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1449
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1450 BTYP_BRANCH (cell, cell);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1451 BTYP_BRANCH (struct, map);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1452 #undef BTYP_BRANCH
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1453
10786
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1454 case btyp_func_handle:
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1455 gripe_wrong_type_arg ("mat2cell", a);
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1456 break;
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1457 default:
146a97c3bc97 some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents: 10756
diff changeset
1458 retval = do_mat2cell (a, d, nargin-1);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10125
diff changeset
1459 }
5784
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1460 }
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1461
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1462 return retval;
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1463 }
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1464
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1465 /*
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1466
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1467 %!test
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1468 %! x = reshape(1:20,5,4);
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1469 %! c = mat2cell(x,[3,2],[3,1]);
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1470 %! assert(c,{[1,6,11;2,7,12;3,8,13],[16;17;18];[4,9,14;5,10,15],[19;20]})
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1471
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1472 %!test
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1473 %! x = 'abcdefghij';
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1474 %! c = mat2cell(x,1,[0,4,2,0,4,0]);
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1475 %! empty1by0str = resize('',1,0);
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1476 %! assert(c,{empty1by0str,'abcd','ef',empty1by0str,'ghij',empty1by0str})
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1477
70f7659d0fb9 [project @ 2006-05-03 05:55:56 by dbateman]
dbateman
parents: 5642
diff changeset
1478 */
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1479
9740
78ac37d73557 fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
1480 // FIXME: it would be nice to allow ranges being handled without a conversion.
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1481 template <class NDA>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1482 static Cell
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1483 do_cellslices_nda (const NDA& array,
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1484 const Array<octave_idx_type>& lb,
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1485 const Array<octave_idx_type>& ub,
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1486 int dim = -1)
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1487 {
9740
78ac37d73557 fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
1488 octave_idx_type n = lb.length ();
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1489 Cell retval (1, n);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1490 if (array.is_vector () && (dim == -1
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
1491 || (dim == 0 && array.columns () == 1)
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1492 || (dim == 1 && array.rows () == 1)))
9057
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1493 {
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1494 for (octave_idx_type i = 0; i < n && ! error_state; i++)
9740
78ac37d73557 fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
1495 retval(i) = array.index (idx_vector (lb(i) - 1, ub(i)));
9057
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1496 }
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1497 else
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1498 {
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1499 const dim_vector dv = array.dims ();
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1500 int ndims = dv.length ();
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1501 if (dim < 0)
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1502 dim = dv.first_non_singleton ();
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1503 ndims = std::max (ndims, dim + 1);
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1504
11570
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
1505 Array<idx_vector> idx (dim_vector (ndims, 1), idx_vector::colon);
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1506
9057
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1507 for (octave_idx_type i = 0; i < n && ! error_state; i++)
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1508 {
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1509 idx(dim) = idx_vector (lb(i) - 1, ub(i));
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1510 retval(i) = array.index (idx);
9057
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1511 }
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1512 }
8b263623d0f3 have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents: 9042
diff changeset
1513
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1514 return retval;
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1515 }
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1516
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1517 DEFUN_DLD (cellslices, args, ,
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1518 "-*- texinfo -*-\n\
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1519 @deftypefn {Loadable Function} {@var{sl} =} cellslices (@var{x}, @var{lb}, @var{ub}, @var{dim})\n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
1520 Given an array @var{x}, this function produces a cell array of slices from\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
1521 the array determined by the index vectors @var{lb}, @var{ub}, for lower and\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
1522 upper bounds, respectively. In other words, it is equivalent to the\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
1523 following code:\n\
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1524 \n\
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1525 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9057
diff changeset
1526 @group\n\
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1527 n = length (lb);\n\
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1528 sl = cell (1, n);\n\
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1529 for i = 1:length (lb)\n\
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1530 sl@{i@} = x(:,@dots{},lb(i):ub(i),@dots{},:);\n\
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1531 endfor\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9057
diff changeset
1532 @end group\n\
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1533 @end example\n\
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1534 \n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
1535 The position of the index is determined by @var{dim}. If not specified,\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
1536 slicing is done along the first non-singleton dimension.\n\
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1537 @end deftypefn")
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1538 {
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1539 octave_value retval;
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1540 int nargin = args.length ();
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1541 if (nargin == 3 || nargin == 4)
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1542 {
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1543 octave_value x = args(0);
9740
78ac37d73557 fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
1544 Array<octave_idx_type> lb = args(1).octave_idx_type_vector_value ();
78ac37d73557 fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
1545 Array<octave_idx_type> ub = args(2).octave_idx_type_vector_value ();
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1546 int dim = -1;
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1547 if (nargin == 4)
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1548 {
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1549 dim = args(3).int_value () - 1;
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1550 if (dim < 0)
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1551 error ("cellslices: DIM must be a valid dimension");
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1552 }
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1553
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1554 if (! error_state)
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1555 {
9740
78ac37d73557 fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
1556 if (lb.length () != ub.length ())
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1557 error ("cellslices: the lengths of LB and UB must match");
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1558 else
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1559 {
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1560 Cell retcell;
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1561 if (! x.is_sparse_type () && x.is_matrix_type ())
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1562 {
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1563 // specialize for some dense arrays.
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1564 if (x.is_bool_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1565 retcell = do_cellslices_nda (x.bool_array_value (), lb, ub, dim);
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1566 else if (x.is_char_matrix ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1567 retcell = do_cellslices_nda (x.char_array_value (), lb, ub, dim);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1568 else if (x.is_integer_type ())
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1569 {
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1570 if (x.is_int8_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1571 retcell = do_cellslices_nda (x.int8_array_value (), lb, ub, dim);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1572 else if (x.is_int16_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1573 retcell = do_cellslices_nda (x.int16_array_value (), lb, ub, dim);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1574 else if (x.is_int32_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1575 retcell = do_cellslices_nda (x.int32_array_value (), lb, ub, dim);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1576 else if (x.is_int64_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1577 retcell = do_cellslices_nda (x.int64_array_value (), lb, ub, dim);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1578 else if (x.is_uint8_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1579 retcell = do_cellslices_nda (x.uint8_array_value (), lb, ub, dim);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1580 else if (x.is_uint16_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1581 retcell = do_cellslices_nda (x.uint16_array_value (), lb, ub, dim);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1582 else if (x.is_uint32_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1583 retcell = do_cellslices_nda (x.uint32_array_value (), lb, ub, dim);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1584 else if (x.is_uint64_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1585 retcell = do_cellslices_nda (x.uint64_array_value (), lb, ub, dim);
9678
c929f09457b7 rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents: 9622
diff changeset
1586 }
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1587 else if (x.is_complex_type ())
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1588 {
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1589 if (x.is_single_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1590 retcell = do_cellslices_nda (x.float_complex_array_value (), lb, ub, dim);
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1591 else
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1592 retcell = do_cellslices_nda (x.complex_array_value (), lb, ub, dim);
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1593 }
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1594 else
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1595 {
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1596 if (x.is_single_type ())
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1597 retcell = do_cellslices_nda (x.float_array_value (), lb, ub, dim);
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1598 else
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1599 retcell = do_cellslices_nda (x.array_value (), lb, ub, dim);
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1600 }
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1601 }
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1602 else
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1603 {
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1604 // generic code.
9740
78ac37d73557 fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents: 9731
diff changeset
1605 octave_idx_type n = lb.length ();
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1606 retcell = Cell (1, n);
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1607 const dim_vector dv = x.dims ();
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1608 int ndims = dv.length ();
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1609 if (dim < 0)
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1610 dim = dv.first_non_singleton ();
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1611 ndims = std::max (ndims, dim + 1);
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1612 octave_value_list idx (ndims, octave_value::magic_colon_t);
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1613 for (octave_idx_type i = 0; i < n && ! error_state; i++)
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1614 {
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1615 idx(dim) = Range (lb(i), ub(i));
8985
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1616 retcell(i) = x.do_index_op (idx);
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1617 }
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1618 }
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1619 if (! error_state)
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1620 retval = retcell;
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1621 }
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1622 }
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1623 }
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1624 else
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1625 print_usage ();
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1626
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1627 return retval;
193804a4f82f add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
1628 }
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1629
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1630 /*
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1631 %!test
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1632 %! m = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12];
10125
82ed1b4c3011 improve cellslices
Jaroslav Hajek <highegg@gmail.com>
parents: 10122
diff changeset
1633 %! c = cellslices (m, [1, 2], [2, 3], 2);
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1634 %! assert (c, {[1, 2; 5, 6; 9, 10], [2, 3; 6, 7; 10, 11]});
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 10075
diff changeset
1635 */
10963
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1636
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1637 DEFUN_DLD (cellindexmat, args, ,
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1638 "-*- texinfo -*-\n\
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1639 @deftypefn {Loadable Function} {@var{y} =} cellindexmat (@var{x}, @var{varargin})\n\
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1640 Given a cell array of matrices @var{x}, this function computes\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1641 \n\
10963
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1642 @example\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1643 @group\n\
10963
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1644 Y = cell (size (X));\n\
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1645 for i = 1:numel (X)\n\
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1646 Y@{i@} = X@{i@}(varargin@{:@});\n\
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1647 endfor\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1648 @end group\n\
10963
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1649 @end example\n\
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1650 @seealso{cellfun, cellslices}\n\
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1651 @end deftypefn")
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1652 {
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1653 octave_value retval;
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1654 if (args.length () >= 1)
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1655 {
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1656 if (args(0).is_cell ())
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1657 {
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1658 const Cell x = args(0).cell_value ();
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1659 NoAlias<Cell> y(x.dims ());
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1660 octave_idx_type nel = x.numel ();
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1661 octave_value_list idx = args.slice (1, args.length () - 1);
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1662
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1663 for (octave_idx_type i = 0; i < nel; i++)
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1664 {
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1665 octave_quit ();
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1666 octave_value tmp = x(i);
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1667 y(i) = tmp.do_index_op (idx);
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1668 if (error_state)
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1669 break;
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1670 }
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1671
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1672 retval = y;
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1673 }
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1674 else
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1675 error ("cellindexmat: X must be a cell");
10963
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1676 }
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1677 else
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1678 print_usage ();
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1679
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1680 return retval;
0d7624852beb implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents: 10846
diff changeset
1681 }