changeset 10963:0d7624852beb

implement cellindexmat function
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 13 Sep 2010 08:23:32 +0200
parents efc0f560e690
children 50273985ebca
files src/ChangeLog src/DLD-FUNCTIONS/cellfun.cc
diffstat 2 files changed, 47 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2010-09-13  Jaroslav Hajek  <highegg@gmail.com>
+
+	* DLD-FUNCTIONS/cellfun.cc (Fcellindexmat): New DEFUN.
+
 2010-09-12  Jaroslav Hajek  <highegg@gmail.com>
 
 	* ov-fcn-handle.cc (octave_fcn_binder::maybe_binder): Stash name tags
--- a/src/DLD-FUNCTIONS/cellfun.cc
+++ b/src/DLD-FUNCTIONS/cellfun.cc
@@ -1577,3 +1577,46 @@
 %! c = cellslices (m, [1, 2], [2, 3], 2);
 %! assert (c, {[1, 2; 5, 6; 9, 10], [2, 3; 6, 7; 10, 11]});
 */
+
+DEFUN_DLD (cellindexmat, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Loadable Function} {@var{y} =} cellindexmat (@var{x}, @var{varargin})\n\
+Given a cell array of matrices @var{x}, this function computes\n\
+@example\n\
+  Y = cell (size (X));\n\
+  for i = 1:numel (X)\n\
+    Y@{i@} = X@{i@}(varargin@{:@});\n\
+  endfor\n\
+@end example\n\
+@seealso{cellfun, cellslices}\n\
+@end deftypefn")
+{
+  octave_value retval;
+  if (args.length () >= 1)
+    {
+      if (args(0).is_cell ())
+        {
+          const Cell x = args(0).cell_value ();
+          NoAlias<Cell> y(x.dims ());
+          octave_idx_type nel = x.numel ();
+          octave_value_list idx = args.slice (1, args.length () - 1);
+
+          for (octave_idx_type i = 0; i < nel; i++)
+            {
+              octave_quit ();
+              octave_value tmp = x(i);
+              y(i) = tmp.do_index_op (idx);
+              if (error_state)
+                break;
+            }
+
+          retval = y;
+        }
+      else
+        error ("cellindexmat: first argument must be a cell");
+    }
+  else
+    print_usage ();
+
+  return retval;
+}