comparison scripts/general/cell2mat.m @ 8107:8655dc0906e6

Special case single type conacation in Fcat. Rework cell2mat to take advantage
author David Bateman <dbateman@free.fr>
date Wed, 17 Sep 2008 14:37:49 -0400
parents 3b2346046d32
children eb63fbe60fab
comparison
equal deleted inserted replaced
8106:8a42498edb30 8107:8655dc0906e6
48 else 48 else
49 error ("cell2mat: all elements of cell array must be numeric, logical or char"); 49 error ("cell2mat: all elements of cell array must be numeric, logical or char");
50 endif 50 endif
51 elseif (ndims (c) == 2) 51 elseif (ndims (c) == 2)
52 nr = rows (c); 52 nr = rows (c);
53 c1 = cell (nr, 1); 53 nc = columns (c);
54 for i = 1 : nr 54 if (nc > nr)
55 c1{i} = [c{i : nr : end}]; 55 c1 = cell (nr, 1);
56 endfor 56 for i = 1 : nr
57 ## This is faster than "c = cat(1, c{:})" 57 c1{i} = [c{i : nr : end}];
58 m = [cellfun(@(x) x.', c1, "UniformOutput", false){:}].'; 58 endfor
59 m = cat (1, c1 {:});
60 else
61 c1 = cell (nc, 1);
62 for i = 1 : nc
63 c1{i} = cat (1, c{(i - 1) * nr + [1 : nr]});
64 endfor
65 m = [c1{:}];
66 endif
59 else 67 else
60 nd = ndims (c); 68 ## n dimensions case
61 for k = nd : -1 : 2 69 for k = ndims (c):-1:2,
62 sz = size (c); 70 sz = size (c);
63 if (k > ndims (c) || sz(end) == 1)
64 continue;
65 endif
66 sz(end) = 1; 71 sz(end) = 1;
67 c1 = cell (sz); 72 c1 = cell (sz);
68 sz = prod (sz); 73 for i = 1:(prod (sz))
69 if (k == 2) 74 c1{i} = cat (k, c{i:(prod (sz)):end});
70 for i = 1 : sz 75 endfor
71 c1{i} = [c{i : sz : end}];
72 endfor
73 else
74 ## This is faster than
75 ## for i = 1:sz, c1{i} = cat (k, c{i:(prod (sz)):end}); endfor
76 idx = [1, k, (3 : (k - 1)), 2, ((k + 1): nd)];
77 c = cellfun(@(x) permute (x, idx), c, "UniformOutput", false);
78 for i = 1: sz
79 c1{i} = ipermute ([c{i : sz : end}], idx);
80 endfor
81 endif
82 c = c1; 76 c = c1;
83 endfor 77 endfor
84 if (numel (c) > 1) 78 m = cat (1, c1{:});
85 idx = [2, 1, 3 : nd];
86 m = ipermute([cellfun(@(x) permute (x, idx), c, "UniformOutput", false){:}], idx);
87 else
88 m = c{1};
89 endif
90 endif 79 endif
80
91 endfunction 81 endfunction
92 82
93 ## Tests 83 ## Tests
94 %!shared C, D, E, F 84 %!shared C, D, E, F
95 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]}; 85 %! C = {[1], [2 3 4]; [5; 9], [6 7 8; 10 11 12]};