annotate toolboxes/FullBNT-1.0.7/KPMtools/cell2num.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function N = cell2num(C)
wolffd@0 2 % CELL2NUM Convert a 2D cell array to a 2D numeric array
wolffd@0 3 % N = cell2num(C)
wolffd@0 4 % If the cells contain column vectors, they must have the same number of rows in each row of C.
wolffd@0 5 % Each column will be concatenated.
wolffd@0 6 %
wolffd@0 7 % Example 1:
wolffd@0 8 % C = num2cell(rand(2,2))
wolffd@0 9 % [0.4565] [0.8214]
wolffd@0 10 % [0.0185] [0.4447]
wolffd@0 11 % N = cell2num(C)
wolffd@0 12 % 0.4565 0.8214
wolffd@0 13 % 0.0185 0.4447
wolffd@0 14 %
wolffd@0 15 % Example 2:
wolffd@0 16 % C = cell(2, 3);
wolffd@0 17 % for i=1:2
wolffd@0 18 % for j=1:3
wolffd@0 19 % C{i,j} = rand(i, 1);
wolffd@0 20 % end
wolffd@0 21 % end
wolffd@0 22 % C =
wolffd@0 23 % [ 0.8998] [ 0.8216] [ 0.6449]
wolffd@0 24 % [2x1 double] [2x1 double] [2x1 double]
wolffd@0 25 % C{2,1} =
wolffd@0 26 % 0.8180
wolffd@0 27 % 0.6602
wolffd@0 28 % N=cell2num(C)
wolffd@0 29 % 0.8998 0.8216 0.6449
wolffd@0 30 % 0.8180 0.3420 0.3412
wolffd@0 31 % 0.6602 0.2897 0.5341
wolffd@0 32
wolffd@0 33
wolffd@0 34 % error('use cell2mat in matlab 7')
wolffd@0 35
wolffd@0 36
wolffd@0 37 if isempty(C)
wolffd@0 38 N = [];
wolffd@0 39 return;
wolffd@0 40 end
wolffd@0 41
wolffd@0 42 if any(cellfun('isempty', C)) %any(isemptycell(C))
wolffd@0 43 error('can''t convert cell array with empty cells to matrix')
wolffd@0 44 end
wolffd@0 45
wolffd@0 46 [nrows ncols] = size(C);
wolffd@0 47 %N = reshape(cat(1, C{:}), [nrows ncols]); % this only works if C only contains scalars
wolffd@0 48 r = 0;
wolffd@0 49 for i=1:nrows
wolffd@0 50 r = r + size(C{i,1}, 1);
wolffd@0 51 end
wolffd@0 52 c = 0;
wolffd@0 53 for j=1:ncols
wolffd@0 54 c = c + size(C{1,j}, 2);
wolffd@0 55 end
wolffd@0 56 N = reshape(cat(1, C{:}), [r c]);
wolffd@0 57