Mercurial > hg > octave-lyh
annotate scripts/general/cell2mat.m @ 9860:c0d0b6e37a36
improve cell2mat
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 24 Nov 2009 13:40:51 +0100 |
parents | eb63fbe60fab |
children | 03d0dea2309d |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2005, 2006, 2007, 2008 Laurent Mazet |
5580 | 2 ## |
7016 | 3 ## This file is part of Octave. |
5580 | 4 ## |
7016 | 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. | |
9 ## | |
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. | |
5580 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5580 | 18 |
19 ## -*- texinfo -*- | |
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. | |
5642 | 25 ## @seealso{mat2cell, num2cell} |
5580 | 26 ## @end deftypefn |
27 | |
28 function m = cell2mat (c) | |
29 | |
30 if (nargin != 1) | |
6046 | 31 print_usage (); |
5580 | 32 endif |
33 | |
34 if (! iscell (c)) | |
35 error ("cell2mat: c is not a cell array"); | |
36 endif | |
37 | |
38 nb = numel (c); | |
39 | |
9860 | 40 ## We only want numeric, logical, and char matrices. |
41 valid = cellfun (@isnumeric, c); | |
42 valid |= cellfun (@islogical, c); | |
43 valid |= cellfun (@ischar, c); | |
44 | |
45 if (! all (valid)) | |
46 error ("cell2mat: elements must be numeric, char or logical"); | |
47 endif | |
48 | |
5580 | 49 if (nb == 0) |
50 m = []; | |
8103
3b2346046d32
improve speed of cell2mat
David Bateman <dbateman@free.fr>
parents:
8102
diff
changeset
|
51 elseif (ndims (c) == 2) |
9860 | 52 ## 2d case optimized |
53 [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
|
54 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
|
55 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
|
56 for i = 1 : nr |
9860 | 57 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
|
58 endfor |
9860 | 59 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
|
60 else |
8655dc0906e6
Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents:
8103
diff
changeset
|
61 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
|
62 for i = 1 : nc |
9860 | 63 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
|
64 endfor |
8655dc0906e6
Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents:
8103
diff
changeset
|
65 m = [c1{:}]; |
8655dc0906e6
Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents:
8103
diff
changeset
|
66 endif |
5580 | 67 else |
8107
8655dc0906e6
Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents:
8103
diff
changeset
|
68 ## 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
|
69 for k = ndims (c):-1:2, |
5580 | 70 sz = size (c); |
9860 | 71 sz(k) = 1; |
5580 | 72 c1 = cell (sz); |
9860 | 73 n1 = prod (sz); |
74 for i = 1:n1 | |
75 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
|
76 endfor |
5580 | 77 c = c1; |
78 endfor | |
8107
8655dc0906e6
Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents:
8103
diff
changeset
|
79 m = cat (1, c1{:}); |
5580 | 80 endif |
8107
8655dc0906e6
Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents:
8103
diff
changeset
|
81 |
5580 | 82 endfunction |
83 | |
84 ## Tests | |
85 %!shared C, D, E, F | |
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); | |
5716 | 91 %!assert (cell2mat (D), F); |
9860 | 92 %!test |
93 %! m = rand (10) + i * rand (10); | |
94 %! c = mat2cell (m, [1 2 3 4], [4 3 2 1]); | |
95 %! assert (cell2mat (c), m) | |
96 %!test | |
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) | |
5580 | 100 ## Demos |
101 %!demo | |
102 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}; | |
103 %! cell2mat (C) |