function [m1,m2] = equalizematrixdimensions(m1,m2) <m1> is a matrix (cell matrix okay) <m2> is a matrix (cell matrix okay) make <m1> and <m2> the same dimensions by expanding these matrices as necessary and filling any new entries with NaN or []. <m1> and <m2> must be of the same type (matrix or cell matrix). example: [f,g] = equalizematrixdimensions([1 2],[1 3]'); isequalwithequalnans(f,[1 2; NaN NaN]) isequalwithequalnans(g,[1 NaN; 3 NaN])
0001 function [m1,m2] = equalizematrixdimensions(m1,m2) 0002 0003 % function [m1,m2] = equalizematrixdimensions(m1,m2) 0004 % 0005 % <m1> is a matrix (cell matrix okay) 0006 % <m2> is a matrix (cell matrix okay) 0007 % 0008 % make <m1> and <m2> the same dimensions by expanding these matrices 0009 % as necessary and filling any new entries with NaN or []. 0010 % <m1> and <m2> must be of the same type (matrix or cell matrix). 0011 % 0012 % example: 0013 % [f,g] = equalizematrixdimensions([1 2],[1 3]'); 0014 % isequalwithequalnans(f,[1 2; NaN NaN]) 0015 % isequalwithequalnans(g,[1 NaN; 3 NaN]) 0016 0017 % calc 0018 dim = max(ndims(m1),ndims(m2)); 0019 0020 % figure out the new dimensions 0021 newdim = zeros(1,dim); 0022 for p=1:dim 0023 newdim(p) = max(size(m1,p),size(m2,p)); 0024 end 0025 0026 % do it 0027 if iscell(m1) 0028 m1 = placematrix2(cell(newdim),m1); 0029 m2 = placematrix2(cell(newdim),m2); 0030 else 0031 m1 = placematrix2(repmat(NaN,newdim),m1); 0032 m2 = placematrix2(repmat(NaN,newdim),m2); 0033 end