annotate scripts/general/cell2mat.m @ 5642:2618a0750ae6

[project @ 2006-03-06 21:26:48 by jwe]
author jwe
date Mon, 06 Mar 2006 21:26:54 +0000
parents 9cde5eb6f18b
children 7f79a99e273e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2005 Laurent Mazet
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
2 ##
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
4 ## it under the terms of the GNU General Public License as published by
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
5 ## the Free Software Foundation; either version 2 of the License, or
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
6 ## (at your option) any later version.
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
7 ##
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful,
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
11 ## GNU General Public License for more details.
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
12 ##
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
14 ## along with this program; if not, write to the Free Software
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
16
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
17 ## -*- texinfo -*-
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
18 ## @deftypefn {Function File} {@var{m} =} cell2mat (@var{c})
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
19 ## Convert the cell array @var{c} into a matrix by concatenating all
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
20 ## elements of @var{c} into a hyperrectangle. Elements of @var{c} must
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
21 ## be numeric, logical or char, and @code{cat} must be able to
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
22 ## concatenate them together.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5580
diff changeset
23 ## @seealso{mat2cell, num2cell}
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
24 ## @end deftypefn
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
25
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
26 function m = cell2mat (c)
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
27
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
28 if (nargin != 1)
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
29 usage ("m = cell2mat (c)");
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
30 endif
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
31
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
32 if (! iscell (c))
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
33 error ("cell2mat: c is not a cell array");
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
34 endif
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
35
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
36 nb = numel (c);
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
37
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
38 if (nb == 0)
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
39 m = [];
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
40 elseif (nb == 1)
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
41 elt = c{1};
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
42 if (isnumeric (elt) || ischar (elt) || islogical (elt))
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
43 m = elt;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
44 elseif (iscell (elt))
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
45 m = cell2mat (elt);
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
46 else
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
47 error ("cell2mat: all elements of cell array must be numeric, logical or char");
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
48 endif
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
49 else
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
50 ## n dimensions case
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
51 for k = ndims (c):-1:2,
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
52 sz = size (c);
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
53 sz(end) = 1;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
54 c1 = cell (sz);
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
55 for i = 1:(prod (sz))
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
56 c1{i} = cat (k, c{i:(prod (sz)):end});
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
57 endfor
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
58 c = c1;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
59 endfor
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
60 m = cat (1, c1{:});
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
61 endif
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
62
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
63 endfunction
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
64
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
65 ## Tests
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
66 %!shared C, D, E, F
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
67 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]};
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
68 %! D = C; D(:,:,2) = C;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
69 %! E = [1 2 3 4; 5 6 7 8; 9 10 11 12];
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
70 %! F = E; F(:,:,2) = E;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
71 %!assert (cell2mat (C), E);
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
72 %!test
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
73 %! if ([1e6,1e4,1] * str2num (split (version, '.')) > 2010064)
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
74 %! assert (cell2mat (D), F); % crashes octave 2.1.64
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
75 %! endif
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
76 ## Demos
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
77 %!demo
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
78 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]};
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
79 %! cell2mat (C)