squish.m 523 B

1234567891011121314151617181920
  1. function f = squish(m,num)
  2. % function f = squish(m,num)
  3. %
  4. % <m> is a matrix
  5. % <num> is the positive number of initial dimensions to squish together
  6. %
  7. % return <m> squished.
  8. %
  9. % example:
  10. % isequal(squish([1 2; 3 4],2),[1 3 2 4]')
  11. % get the size of m
  12. msize = [size(m) ones(1,num-ndims(m))]; % add ones to end if necessary
  13. % calculate the new dimensions
  14. newdim = [prod(msize(1:num)) msize(num+1:end)];
  15. % do the reshape
  16. f = reshape(m,[newdim 1]); % tack on a 1 to handle the special case of squishing everything together