annotate scripts/general/cell2mat.m @ 11188:4cb1522e4d0f

Use function handle as input to cellfun, rather than quoted function name or anonymous function wrapper.
author Rik <octave@nomad.inbox5.com>
date Wed, 03 Nov 2010 17:20:56 -0700
parents 5a55773fcaa9
children b1f4bdc276b6
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
10784
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
2 ## Copyright (C) 2010 Jaroslav Hajek
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
3 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
4 ## This file is part of Octave.
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
5 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
6 ## 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
7 ## 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
8 ## 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
9 ## your option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
10 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
11 ## 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
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
13 ## 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
14 ## General Public License for more details.
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
15 ##
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
16 ## 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
17 ## 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
18 ## <http://www.gnu.org/licenses/>.
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
19
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {@var{m} =} cell2mat (@var{c})
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
22 ## 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
23 ## 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
24 ## 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
25 ## must be able to concatenate them together.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5580
diff changeset
26 ## @seealso{mat2cell, num2cell}
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
27 ## @end deftypefn
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
28
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
29 function m = cell2mat (c)
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
30
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
31 if (nargin != 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5720
diff changeset
32 print_usage ();
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
33 endif
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
34
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
35 if (! iscell (c))
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
36 error ("cell2mat: c is not a cell array");
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
37 endif
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
38
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
39 nb = numel (c);
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
40
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
41 ## We only want numeric, logical, and char matrices.
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
42 valid = cellfun (@isnumeric, c);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
43 valid |= cellfun (@islogical, c);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
44 valid |= cellfun (@ischar, c);
10441
03d0dea2309d support cells of cells in cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 9860
diff changeset
45 validc = cellfun (@iscell, c);
10863
5a55773fcaa9 allow structs in cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 10784
diff changeset
46 valids = cellfun (@isstruct, c);
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
47
10863
5a55773fcaa9 allow structs in cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 10784
diff changeset
48 if (! all (valid(:)) && ! all (validc(:)) && ! all (valids(:)))
5a55773fcaa9 allow structs in cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 10784
diff changeset
49 error ("cell2mat: wrong type elements or mixed cells, structs and matrices");
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
50 endif
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
51
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
52 if (nb == 0)
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
53 m = [];
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
54 else
10784
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
55 ## The goal is to minimize the total number of cat() calls.
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
56 ## The dimensions can be concatenated along in arbitrary order.
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
57 ## The numbers of concatenations are:
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
58 ## n / d1
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
59 ## n / (d1 * d2)
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
60 ## n / (d1 * d2 * d3)
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
61 ## etc.
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
62 ## This is minimized if d1 >= d2 >= d3...
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
63
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
64 sc = size (c);
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
65 nd = ndims (c);
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
66 [~, isc] = sort (sc);
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
67 for idim = isc
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
68 if (sc(idim) == 1)
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
69 continue;
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
70 endif
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
71 xdim = [1:idim-1, idim+1:nd];
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
72 cc = num2cell (c, xdim);
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
73 c = cellfun (@cat, {idim}, cc{:}, "uniformoutput", false);
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
74 endfor
10784
ca2df6737d6b generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents: 10549
diff changeset
75 m = c{1};
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
76 endif
8107
8655dc0906e6 Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents: 8103
diff changeset
77
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
78 endfunction
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
79
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
80 ## Tests
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
81 %!shared C, D, E, F
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
82 %! 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
83 %! D = C; D(:,:,2) = C;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
84 %! 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
85 %! F = E; F(:,:,2) = E;
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
86 %!assert (cell2mat (C), E);
5716
7f79a99e273e [project @ 2006-03-24 16:56:09 by jwe]
jwe
parents: 5642
diff changeset
87 %!assert (cell2mat (D), F);
9860
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
88 %!test
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
89 %! m = rand (10) + i * rand (10);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
90 %! c = mat2cell (m, [1 2 3 4], [4 3 2 1]);
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
91 %! assert (cell2mat (c), m)
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
92 %!test
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
93 %! m = int8 (256*rand (4, 5, 6, 7, 8));
c0d0b6e37a36 improve cell2mat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
94 %! 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
95 %! assert (cell2mat (c), m)
10460
4975d63bb2df cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents: 10441
diff changeset
96 %!test
4975d63bb2df cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents: 10441
diff changeset
97 %! m = {1, 2, 3};
4975d63bb2df cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents: 10441
diff changeset
98 %! assert (cell2mat (mat2cell (m, 1, [1 1 1])), m);
5580
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
99 ## Demos
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
100 %!demo
9cde5eb6f18b [project @ 2005-12-14 18:21:55 by jwe]
jwe
parents:
diff changeset
101 %! 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
102 %! cell2mat (C)