forked from pdollar/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell2array.m
55 lines (52 loc) · 1.8 KB
/
cell2array.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function Y = cell2array( X, padEl )
% Flattens a cell array of regular arrays into a regular array.
%
% Each element of X must be a regular array, and must have the same number
% of dimensions k. Converts X to an array Y of dimension k+1 where
% Y(:,:...,:,i) is X{i} padded to be as big as the biggest element in X
% (along each dimension). Specifically, let di1..dik be the k dimensions of
% element X{i}. Let dj=max(dij) for each j. Then each element of X{i} is
% padded to have size [d1 ... dk], and then the elements of X are stacked
% into a vector. Treats the cell array X as a vector (so ignores the
% layout of X).
%
% USAGE
% Y = cell2array( X, [padEl] )
%
% INPUTS
% X - cell array of regular arrays each with dimension k
% padEl - [0] element with which to pad
%
% OUTPUTS
% Y - resulting array of dimension k+1
%
% EXAMPLE
% for i=1:10; X{i}=rand(30); end; Y = cell2array(X);
%
% See also MAT2CELL2, CELL2MAT
%
% Piotr's Computer Vision Matlab Toolbox Version 2.40
% Copyright 2014 Piotr Dollar. [pdollar-at-gmail.com]
% Licensed under the Simplified BSD License [see external/bsd.txt]
if(~iscell(X)); error('X must be a cell array'); end
if(iscell(X{1})); error('X must contain regular arrays'); end
n = numel(X); nd = ndims(X{1});
for i=2:n
if(ndims(X{i})~=nd); error('all elem of X must have same dims'); end
end
if(nargin<2); padEl=0; end
%%% get maximum and minimum size of any element of X
maxsiz = size(X{1}); minsiz = size(X{1});
for i=1:n
siz = size(X{i});
maxsiz = max( maxsiz, siz );
minsiz = min( minsiz, siz );
end
%%% construct Y
inds=cell(1,nd); for d=1:nd; inds{d} = 1:maxsiz(d); end
Y = zeros( [maxsiz n], class(X{1}) );
if( all(maxsiz==minsiz) )
for i=1:n; Y( inds{:}, i ) = X{i}; end
else
for i=1:n; Y( inds{:}, i ) = arrayToDims( X{i}, maxsiz, padEl); end
end