1## Copyright (C) 2005, 2006, 2007, 2008 Laurent Mazet
3## This file is part of Octave.
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
20## @deftypefn {Function File} {@var{m} =} cell2mat (@var{c})
21## Convert the cell array @var{c} into a matrix by concatenating all
22## elements of @var{c} into a hyperrectangle. Elements of @var{c} must
23## be numeric, logical or char, and @code{cat} must be able to
24## concatenate them together.
25## @seealso{mat2cell, num2cell}
28function m = cell2mat (c)
35 error ("cell2mat: c is not a cell array");
40 ## We only want numeric, logical, and char matrices.
41 valid = cellfun (@isnumeric, c);
42 valid |= cellfun (@islogical, c);
43 valid |= cellfun (@ischar, c);
46 error ("cell2mat: elements must be numeric, char or logical");
51 elseif (ndims (c) == 2)
63 c1{i} = vertcat (c{:,i});
69 for k = ndims (c):-1:2,
75 c1{i} = cat (k, c{i:n1:end});
86%! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]};
87%! D = C; D(:,:,2) = C;
88%! E = [1 2 3 4; 5 6 7 8; 9 10 11 12];
89%! F = E; F(:,:,2) = E;
90%!assert (cell2mat (C), E);
91%!assert (cell2mat (D), F);
93%! m = rand (10) + i * rand (10);
94%! c = mat2cell (m, [1 2 3 4], [4 3 2 1]);
95%! assert (cell2mat (c), m)
97%! m = int8 (256*rand (4, 5, 6, 7, 8));
98%! c = mat2cell (m, [1 2 1], [1 2 2], [3 1 1 1], [4 1 2], [3 1 4]);
99%! assert (cell2mat (c), m)
102%! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]};