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