annotate src/DLD-FUNCTIONS/cellfun.cc @ 10521:4d1fc073fbb7

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