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

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