equalizematrixdimensions.m 874 B

123456789101112131415161718192021222324252627282930313233
  1. function [m1,m2] = equalizematrixdimensions(m1,m2)
  2. % function [m1,m2] = equalizematrixdimensions(m1,m2)
  3. %
  4. % <m1> is a matrix (cell matrix okay)
  5. % <m2> is a matrix (cell matrix okay)
  6. %
  7. % make <m1> and <m2> the same dimensions by expanding these matrices
  8. % as necessary and filling any new entries with NaN or [].
  9. % <m1> and <m2> must be of the same type (matrix or cell matrix).
  10. %
  11. % example:
  12. % [f,g] = equalizematrixdimensions([1 2],[1 3]');
  13. % isequalwithequalnans(f,[1 2; NaN NaN])
  14. % isequalwithequalnans(g,[1 NaN; 3 NaN])
  15. % calc
  16. dim = max(ndims(m1),ndims(m2));
  17. % figure out the new dimensions
  18. newdim = zeros(1,dim);
  19. for p=1:dim
  20. newdim(p) = max(size(m1,p),size(m2,p));
  21. end
  22. % do it
  23. if iscell(m1)
  24. m1 = placematrix2(cell(newdim),m1);
  25. m2 = placematrix2(cell(newdim),m2);
  26. else
  27. m1 = placematrix2(repmat(NaN,newdim),m1);
  28. m2 = placematrix2(repmat(NaN,newdim),m2);
  29. end