reshape2D.m 557 B

123456789101112131415161718192021
  1. function f = reshape2D(m,dim)
  2. % function f = reshape2D(m,dim)
  3. %
  4. % <m> is a matrix
  5. % <dim> is a dimension of <m>
  6. %
  7. % shift dimension <dim> to the beginning,
  8. % then reshape to be a 2D matrix.
  9. % see also reshape2D_undo.m.
  10. %
  11. % example:
  12. % a = randn(3,4,5);
  13. % b = reshape2D(a,2);
  14. % isequal(size(b),[4 15])
  15. % what is the new permutation order? (be careful that <dim> might be larger than number of dimensions of <m>!)
  16. dimorder = [dim setdiff(1:max(ndims(m),dim),dim)];
  17. % permute and then squish into a 2D matrix
  18. f = reshape(permute(m,dimorder),size(m,dim),[]);