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