1
// Copyright (C) 2010 Olaf Till <olaf.till@uni-jena.de>
3
// This program is free software; you can redistribute it and/or modify it under
4
// the terms of the GNU General Public License as published by the Free Software
5
// Foundation; either version 3 of the License, or (at your option) any later
8
// This program is distributed in the hope that it will be useful, but WITHOUT
9
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13
// You should have received a copy of the GNU General Public License along with
14
// this program; if not, see <http://www.gnu.org/licenses/>.
16
#include <octave/oct.h>
18
DEFUN_DLD (cell2cell, args, ,
20
@deftypefn {Loadable Function} {} cell2cell (@var{c}, @var{dim})\n\
21
Return a one-dimensional cell array, extending along dimension @var{dim},\n\
22
which contains the slices of cell array @var{c} vertical to dimension\n\
26
std::string fname ("cell2cell");
28
if (args.length () != 2)
31
return octave_value_list ();
34
Cell c = args(0).cell_value ();
37
error ("%s: first argument must be a cell array", fname.c_str ());
38
return octave_value_list ();
41
octave_idx_type dim = args(1).int_value ();
44
error ("%s: second argument must be an integer",
46
return octave_value_list ();
51
dim_vector cdims (c.dims ());
53
octave_idx_type n_cdims = cdims.length ();
55
dim_vector rdims, tdims (cdims);
57
octave_idx_type nr = 1;
59
if (n_cdims >= dim && cdims(dim - 1) > 1)
70
for (i = 0; i < dim - 1; i++)
73
rdims(dim - 1) = (nr = cdims(dim - 1));
77
tdims.chop_trailing_singletons ();
89
octave_idx_type nt = tdims.numel ();
91
octave_idx_type base = 0, origin, cursor;
93
octave_idx_type skip_count = 1; // when to skip
95
octave_idx_type min_tp = n_cdims < dim - 1 ? n_cdims : dim - 1;
97
for (i = 0; i < min_tp; i++)
98
skip_count *= cdims(i);
100
octave_idx_type skip = skip_count; // how much to skip
103
skip *= cdims(dim - 1);
105
for (i = 0; i < nr; i++)
113
for (j = 0; j < nt; j++)
115
t(j) = c(origin + cursor++);
117
if (cursor == skip_count)
130
return octave_value (retval);