Mercurial > hg > octave-lyh
annotate scripts/general/cell2mat.m @ 12604:132c89bb44e3
maint: Refactor general/isXXX.m scripts to put input validation first.
* general/isdir.m, general/isequal.m, general/isequalwithequalnans.m,
general/isscalar.m, general/issquare.m, general/isvector.m: Refactor
code to put input validation first.
* general/iscolumn.m, general/isrow.m : Remove useless initialization
of output variable.
* general/isa.m: Add additional tests for various classes.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 13 Apr 2011 09:32:10 -0700 |
parents | c792872f8942 |
children | cefd568ea073 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2005-2011 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 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
24 ## be numeric, logical or char matrices, or cell arrays, and @code{cat} |
10441
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 | 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 |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
45 ## We only want numeric, logical, and char matrices. |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
46 valid = cellfun (@isnumeric, c); |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
47 valid |= cellfun (@islogical, c); |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
48 valid |= cellfun (@ischar, c); |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
49 validc = cellfun (@iscell, c); |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
50 valids = cellfun (@isstruct, c); |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
51 |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
52 if (! all (valid(:)) && ! all (validc(:)) && ! all (valids(:))) |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
53 error ("cell2mat: wrong type elements or mixed cells, structs and matrices"); |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
54 endif |
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
55 |
10784
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
56 ## 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
|
57 ## 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
|
58 ## The numbers of concatenations are: |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
59 ## n / d1 |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
60 ## n / (d1 * d2) |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
61 ## n / (d1 * d2 * d3) |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
62 ## etc. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
63 ## This is minimized if d1 >= d2 >= d3... |
10784
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
64 |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
65 sc = size (c); |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
66 nd = ndims (c); |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
67 [~, isc] = sort (sc); |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
68 for idim = isc |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
69 if (sc(idim) == 1) |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
70 continue; |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
71 endif |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
72 xdim = [1:idim-1, idim+1:nd]; |
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
73 cc = num2cell (c, xdim); |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
11190
diff
changeset
|
74 c = cellfun (@cat, {idim}, cc{:}, "uniformoutput", false); |
5580 | 75 endfor |
10784
ca2df6737d6b
generalize cell2mat optimization to n dimensions
Jaroslav Hajek <highegg@gmail.com>
parents:
10549
diff
changeset
|
76 m = c{1}; |
5580 | 77 endif |
8107
8655dc0906e6
Special case single type conacation in Fcat. Rework cell2mat to take advantage
David Bateman <dbateman@free.fr>
parents:
8103
diff
changeset
|
78 |
5580 | 79 endfunction |
80 | |
81 ## Tests | |
82 %!shared C, D, E, F | |
83 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}; | |
84 %! D = C; D(:,:,2) = C; | |
85 %! E = [1 2 3 4; 5 6 7 8; 9 10 11 12]; | |
86 %! F = E; F(:,:,2) = E; | |
87 %!assert (cell2mat (C), E); | |
5716 | 88 %!assert (cell2mat (D), F); |
9860 | 89 %!test |
90 %! m = rand (10) + i * rand (10); | |
91 %! c = mat2cell (m, [1 2 3 4], [4 3 2 1]); | |
92 %! assert (cell2mat (c), m) | |
93 %!test | |
94 %! m = int8 (256*rand (4, 5, 6, 7, 8)); | |
95 %! c = mat2cell (m, [1 2 1], [1 2 2], [3 1 1 1], [4 1 2], [3 1 4]); | |
96 %! assert (cell2mat (c), m) | |
10460
4975d63bb2df
cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents:
10441
diff
changeset
|
97 %!test |
4975d63bb2df
cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents:
10441
diff
changeset
|
98 %! m = {1, 2, 3}; |
4975d63bb2df
cell2mat.m: Add test for cells of cells.
Ben Abbott <bpabbott@mac.com>
parents:
10441
diff
changeset
|
99 %! assert (cell2mat (mat2cell (m, 1, [1 1 1])), m); |
11285
107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
Ben Abbott <bpabbott@mac.com>
parents:
11191
diff
changeset
|
100 %!assert (cell2mat ({}), []); |
5580 | 101 ## Demos |
102 %!demo | |
103 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}; | |
104 %! cell2mat (C) |