Home > NoiseTools > nt_cell2mat.m

nt_cell2mat

PURPOSE ^

y=nt_cell2mat(x) - convert cell matrix of nD matrices to (n+1)D matrix

SYNOPSIS ^

function y=nt_cell2mat(x)

DESCRIPTION ^

y=nt_cell2mat(x) - convert cell matrix of nD matrices to (n+1)D matrix

   y: (n+1)-dimension matrix

   x: cell matrix of matrices

 Similar to matlab's cell2mat() but much more flexible.

 If matrices in x are of different dimensions or sizes, the result
 dimension/size is based on the largest dimension/size. Maximum ndims is 4.

 The cell matrix x can have ndims up to 4.

 NoiseTools.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function y=nt_cell2mat(x)
0002 %y=nt_cell2mat(x) - convert cell matrix of nD matrices to (n+1)D matrix
0003 %
0004 %   y: (n+1)-dimension matrix
0005 %
0006 %   x: cell matrix of matrices
0007 %
0008 % Similar to matlab's cell2mat() but much more flexible.
0009 %
0010 % If matrices in x are of different dimensions or sizes, the result
0011 % dimension/size is based on the largest dimension/size. Maximum ndims is 4.
0012 %
0013 % The cell matrix x can have ndims up to 4.
0014 %
0015 % NoiseTools.
0016 nt_greetings
0017 
0018 MAXDIMS=4; % modify code if more are needed
0019 
0020 if ~iscell(x); error('!'); end
0021 
0022 % recurse if x is matrix of cells
0023 if min(size(x))>1
0024     if ndims(x)>MAXDIMS; error('!'); end
0025     sz=size(x);
0026     yy={};
0027     for k=1:size(x,1)
0028         yy{k}=nt_cell2mat(x(k,:,:,:)); % add more colons if needed
0029     end
0030     y=nt_cell2mat(yy);
0031     return
0032 end
0033         
0034 % find size of largest matrix in cell array
0035 nd=0;
0036 for iTrial=1:numel(x)
0037     nd=max(nd,ndims(x{iTrial})); % find largest number of dimensions
0038 end
0039 if nd>MAXDIMS; error('!'); end
0040 szs=zeros(1,nd);
0041 for iTrial=1:numel(x)
0042     tmp=size(x{iTrial});
0043     szs(1:numel(tmp))=max(szs(1:numel(tmp)),tmp); % find largest size
0044 end
0045     
0046 % transfer to matrix
0047 y=zeros([szs,numel(x)]);
0048 for iTrial=1:numel(x)
0049     sz=size(x{iTrial});
0050     sz(numel(sz)+1:nd)=1;
0051     if nd==1
0052         y(1:sz(1),iTrial)=x{iTrial};
0053     elseif nd==2
0054         y(1:sz(1),1:sz(2),iTrial)=x{iTrial};
0055     elseif nd==3
0056         y(1:sz(1),1:sz(2),1:sz(3),iTrial)=x{iTrial};
0057     elseif nd==2
0058         y(1:sz(1),1:sz(2),1:sz(3),1:sz(4),iTrial)=x{iTrial};
0059     end % add more cases if needed
0060 end
0061 
0062 
0063 % test code
0064 if 0
0065     % basic case: cell array of 2 matrices, same size
0066     x{1}=randn(10);
0067     x{2}=randn(10);
0068     disp(size(nt_cell2mat(x)));
0069 end
0070 if 0
0071     % cell array of 2 matrices, different size
0072     x{1}=randn(10);
0073     x{2}=randn(20);
0074     disp(size(nt_cell2mat(x)));
0075 end
0076 if 0
0077     % cell array of 2 3-D matrices, same size
0078     x{1}=randn(10,11,12);
0079     x{2}=randn(10,11,12);
0080     disp(size(nt_cell2mat(x)));
0081 end
0082 if 0
0083     % cell array with one 2-D matrix and one 3-D matrix
0084     x{1}=randn(10);
0085     x{2}=randn(10,11,12);
0086     disp(size(nt_cell2mat(x)));
0087 end
0088 if 0
0089     % cell array of 2 matrices, same size, plot
0090     x{1}=diag(1:10);
0091     x{2}=diag(randn(10,1));
0092     y=nt_cell2mat(x);
0093     figure(1); clf;
0094     subplot 221
0095     nt_imagescc(x{1});
0096     subplot 222
0097     nt_imagescc(x{2});
0098     subplot 223
0099     nt_imagescc(y(:,:,1));
0100     subplot 224
0101     nt_imagescc(y(:,:,2));
0102 end
0103 if 0
0104     % cell array of 2 matrices, different size, plot
0105     x{1}=diag(1:10);
0106     x{2}=diag(randn(20,1));
0107     y=nt_cell2mat(x);
0108     figure(1); clf;
0109     subplot 221
0110     nt_imagescc(x{1});
0111     subplot 222
0112     nt_imagescc(x{2});
0113     subplot 223
0114     nt_imagescc(y(:,:,1));
0115     subplot 224
0116     nt_imagescc(y(:,:,2));
0117 end
0118 if 0
0119     % 3-D matrix of 2-D matrices, same size
0120     x{1,1}=randn(10);
0121     x{2,1}=randn(10);
0122     x{1,2}=randn(10);
0123     x{2,2}=randn(10);
0124     disp(size(nt_cell2mat(x)));
0125 end
0126 if 0
0127     % 3-D matrix of 2-D matrices, various sizes
0128     x{1,1}=randn(10);
0129     x{2,1}=randn(11);
0130     x{1,2}=randn(12);
0131     x{2,2}=randn(13);
0132     disp(size(nt_cell2mat(x)));
0133 end
0134 
0135

Generated on Sat 29-Apr-2023 17:15:46 by m2html © 2005