annotate scripts/general/cell2mat.m @ 10460:4975d63bb2df

cell2mat.m: Add test for cells of cells.
author Ben Abbott <bpabbott@mac.com>
date Thu, 25 Mar 2010 07:40:27 -0400
parents 03d0dea2309d
children 95c3e38098bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8107
diff changeset
1 ## Copyright (C) 2005, 2006, 2007, 2008 Laurent Mazet
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
2 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
3 ## This file is part of Octave.
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
4 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
6 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
13 ## General Public License for more details.
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
14 ##
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
15 ## 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: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
18
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{m} =} cell2mat (@var{c})
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
21 ## 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
22 ## elements of @var{c} into a hyperrectangle. Elements of @var{c} must
10441
03d0dea2309d support cells of cells in cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 9860
diff changeset
23 ## be numeric, logical or char matrices, or cell arrays, and @code{cat}
03d0dea2309d support cells of cells in cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 9860
diff changeset
24 ## must be able to concatenate them together.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5580
diff changeset
25 ## @seealso{mat2cell, num2cell}
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
26 ## @end deftypefn
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 function m = cell2mat (c)
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
29
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
30 if (nargin != 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5720
diff changeset
31 print_usage ();
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
32 endif
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
33
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
34 if (! iscell (c))
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
35 error ("cell2mat: c is not a cell array");
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
36 endif
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 nb = numel (c);
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
39
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
40 ## We only want numeric, logical, and char matrices.
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
41 valid = cellfun (@isnumeric, c);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
42 valid |= cellfun (@islogical, c);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
43 valid |= cellfun (@ischar, c);
10441
03d0dea2309d support cells of cells in cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 9860
diff changeset
44 validc = cellfun (@iscell, c);
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
45
10441
03d0dea2309d support cells of cells in cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 9860
diff changeset
46 if (! all (valid(:)) && ! all (validc(:)))
03d0dea2309d support cells of cells in cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 9860
diff changeset
47 error ("cell2mat: wrong type elements or mixed cells and matrices");
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
48 endif
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
49
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
50 if (nb == 0)
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
51 m = [];
8103
3b2346046d32 improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents: 8102
diff changeset
52 elseif (ndims (c) == 2)
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
53 ## 2d case optimized
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
54 [nr, nc] = size (c);
8107
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
55 if (nc > nr)
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
56 c1 = cell (nr, 1);
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
57 for i = 1 : nr
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
58 c1{i} = [c{i,:}];
8107
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
59 endfor
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
60 m = vertcat (c1 {:});
8107
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
61 else
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
62 c1 = cell (nc, 1);
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
63 for i = 1 : nc
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
64 c1{i} = vertcat (c{:,i});
8107
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
65 endfor
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
66 m = [c1{:}];
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
67 endif
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
68 else
8107
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
69 ## n dimensions case
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
70 for k = ndims (c):-1:2,
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
71 sz = size (c);
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
72 sz(k) = 1;
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
73 c1 = cell (sz);
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
74 n1 = prod (sz);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
75 for i = 1:n1
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
76 c1{i} = cat (k, c{i:n1:end});
8107
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
77 endfor
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
78 c = c1;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
79 endfor
8107
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
80 m = cat (1, c1{:});
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
81 endif
8107
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
82
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
83 endfunction
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
84
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
85 ## Tests
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
86 %!shared C, D, E, F
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
87 %! 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
88 %! D = C; D(:,:,2) = C;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
89 %! 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
90 %! F = E; F(:,:,2) = E;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
91 %!assert (cell2mat (C), E);
5716
7f79a99e273e [project @ 2006-03-24 16:56:09 by jwe]
jwe
parents: 5642
diff changeset
92 %!assert (cell2mat (D), F);
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
93 %!test
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
94 %! m = rand (10) + i * rand (10);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
95 %! c = mat2cell (m, [1 2 3 4], [4 3 2 1]);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
96 %! assert (cell2mat (c), m)
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
97 %!test
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
98 %! m = int8 (256*rand (4, 5, 6, 7, 8));
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
99 %! c = mat2cell (m, [1 2 1], [1 2 2], [3 1 1 1], [4 1 2], [3 1 4]);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
100 %! assert (cell2mat (c), m)
10460
4975d63bb2df cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents: 10441
diff changeset
101 %!test
4975d63bb2df cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents: 10441
diff changeset
102 %! m = {1, 2, 3};
4975d63bb2df cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents: 10441
diff changeset
103 %! assert (cell2mat (mat2cell (m, 1, [1 1 1])), m);
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
104 ## Demos
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
105 %!demo
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
106 %! 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
107 %! cell2mat (C)