Mercurial > hg > octave-lyh
diff scripts/general/cell2mat.m @ 11285:107e7476a5da
cell2mat.m: Return empty matrix for empty cell.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sun, 21 Nov 2010 13:14:25 -0500 |
parents | 01ddaedd6ad5 |
children | 1740012184f9 |
line wrap: on
line diff
--- a/scripts/general/cell2mat.m +++ b/scripts/general/cell2mat.m @@ -38,20 +38,21 @@ nb = numel (c); - ## We only want numeric, logical, and char matrices. - valid = cellfun (@isnumeric, c); - valid |= cellfun (@islogical, c); - valid |= cellfun (@ischar, c); - validc = cellfun (@iscell, c); - valids = cellfun (@isstruct, c); - - if (! all (valid(:)) && ! all (validc(:)) && ! all (valids(:))) - error ("cell2mat: wrong type elements or mixed cells, structs and matrices"); - endif - if (nb == 0) m = []; else + + ## We only want numeric, logical, and char matrices. + valid = cellfun (@isnumeric, c); + valid |= cellfun (@islogical, c); + valid |= cellfun (@ischar, c); + validc = cellfun (@iscell, c); + valids = cellfun (@isstruct, c); + + if (! all (valid(:)) && ! all (validc(:)) && ! all (valids(:))) + error ("cell2mat: wrong type elements or mixed cells, structs and matrices"); + endif + ## The goal is to minimize the total number of cat() calls. ## The dimensions can be concatenated along in arbitrary order. ## The numbers of concatenations are: @@ -96,6 +97,7 @@ %!test %! m = {1, 2, 3}; %! assert (cell2mat (mat2cell (m, 1, [1 1 1])), m); +%!assert (cell2mat ({}), []); ## Demos %!demo %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]};