Mercurial > hg > octave-lyh
annotate scripts/general/cell2mat.m @ 17535:c12c688a35ed default tip lyh
Fix warnings
author | LYH <lyh.kernel@gmail.com> |
---|---|
date | Fri, 27 Sep 2013 17:43:27 +0800 |
parents | 19b7c7412a63 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12931
diff
changeset
|
1 ## Copyright (C) 2005-2012 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 | 3 ## |
7016 | 4 ## This file is part of Octave. |
5580 | 5 ## |
7016 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
5580 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5580 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {@var{m} =} cell2mat (@var{c}) | |
22 ## Convert the cell array @var{c} into a matrix by concatenating all | |
23 ## elements of @var{c} into a hyperrectangle. Elements of @var{c} must | |
17004
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
24 ## be numeric, logical, or char matrices; or cell arrays; or structs; and |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
25 ## @code{cat} must be able to concatenate them together. |
5642 | 26 ## @seealso{mat2cell, num2cell} |
5580 | 27 ## @end deftypefn |
28 | |
29 function m = cell2mat (c) | |
30 | |
31 if (nargin != 1) | |
6046 | 32 print_usage (); |
5580 | 33 endif |
34 | |
35 if (! iscell (c)) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11285
diff
changeset
|
36 error ("cell2mat: C is not a cell array"); |
5580 | 37 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
38 |
5580 | 39 nb = numel (c); |
40 | |
41 if (nb == 0) | |
42 m = []; | |
43 else | |
11285
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
44 |
17004
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
45 ## Check first for valid matrix types |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
46 valid = cellfun ("isnumeric", c); |
17004
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
47 valid = cellfun ("islogical", c(! valid)); |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
48 valid = cellfun ("isclass", c(! valid), "char"); |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
49 if (! all (valid(:))) |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
50 valid = cellfun ("isclass", c, "cell"); |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
51 if (! all (valid(:))) |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
52 valid = cellfun ("isclass", c, "struct"); |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
53 if (! all (valid(:))) |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
54 error ("cell2mat: wrong type elements or mixed cells, structs, and matrices"); |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
55 endif |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
56 endif |
11285
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
57 endif |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
58 |
17004
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
59 sz = size (c); |
17001
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
60 if (all (cellfun ("numel", c)(:) == 1)) |
17004
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
61 ## Special case of all scalars |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
62 m = reshape (cat (1, c{:}), sz); |
17001
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
63 else |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
64 |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
65 ## The goal is to minimize the total number of cat() calls. |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
66 ## The dimensions can be concatenated along in arbitrary order. |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
67 ## The numbers of concatenations are: |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
68 ## n / d1 |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
69 ## n / (d1 * d2) |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
70 ## n / (d1 * d2 * d3) |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
71 ## etc. |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
72 ## This is minimized if d1 >= d2 >= d3... |
10784
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
73 |
17001
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
74 nd = ndims (c); |
17004
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
75 [~, isz] = sort (sz, "descend"); |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
76 for idim = isz |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
77 if (sz(idim) == 1) |
17001
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
78 continue; |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
79 endif |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
80 xdim = [1:idim-1, idim+1:nd]; |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
81 cc = num2cell (c, xdim); |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
82 c = cellfun ("cat", {idim}, cc{:}, "uniformoutput", false); |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
83 endfor |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
84 m = c{1}; |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
85 |
234519448e76
make cell2mat.m more efficient for large arrays (bug #39387).
Olaf Till <i7tiol@t-online.de>
parents:
14237
diff
changeset
|
86 endif |
5580 | 87 endif |
8107
8655dc0906e6
Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents:
8103
diff
changeset
|
88 |
5580 | 89 endfunction |
90 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 %!demo |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
93 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
94 %! cell2mat (C) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
95 |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
96 %!assert (cell2mat ({}), []); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
97 %!test |
5580 | 98 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}; |
99 %! D = C; D(:,:,2) = C; | |
100 %! E = [1 2 3 4; 5 6 7 8; 9 10 11 12]; | |
101 %! F = E; F(:,:,2) = E; | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 %! assert (cell2mat (C), E); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
103 %! assert (cell2mat (D), F); |
9860 | 104 %!test |
105 %! m = rand (10) + i * rand (10); | |
106 %! c = mat2cell (m, [1 2 3 4], [4 3 2 1]); | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
107 %! assert (cell2mat (c), m); |
9860 | 108 %!test |
109 %! m = int8 (256*rand (4, 5, 6, 7, 8)); | |
110 %! c = mat2cell (m, [1 2 1], [1 2 2], [3 1 1 1], [4 1 2], [3 1 4]); | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
111 %! assert (cell2mat (c), m); |
10460
4975d63bb2df
cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents:
10441
diff
changeset
|
112 %!test |
4975d63bb2df
cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents:
10441
diff
changeset
|
113 %! m = {1, 2, 3}; |
4975d63bb2df
cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents:
10441
diff
changeset
|
114 %! assert (cell2mat (mat2cell (m, 1, [1 1 1])), m); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
115 |
17004
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
116 %!error cell2mat () |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
117 %!error cell2mat (1,2) |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
118 %!error <C is not a cell array> cell2mat ([1,2]) |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
119 %!error <mixed cells, structs, and matrices> cell2mat ({[1], struct()}) |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
120 %!error <mixed cells, structs, and matrices> cell2mat ({[1], {1}}) |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
121 %!error <mixed cells, structs, and matrices> cell2mat ({struct(), {1}}) |
19b7c7412a63
cell2mat.m: Re-vamp input validation for 30% speedup
Rik <rik@octave.org>
parents:
17001
diff
changeset
|
122 |