function m = reshape2D_undo(f,dim,msize) <f> has the same dimensions as the output of reshape2D <dim> was the dimension of <m> that was used in reshape2D <msize> was the size of <m> return <f> but with the same dimensions as passed to reshape2D. example: a = randn(3,4,5); b = reshape2D(a,2); isequal(size(b),[4 15]) c = reshape2D_undo(b,2,size(a)); isequal(size(c),[3 4 5]) isequal(a,c)
0001 function m = reshape2D_undo(f,dim,msize) 0002 0003 % function m = reshape2D_undo(f,dim,msize) 0004 % 0005 % <f> has the same dimensions as the output of reshape2D 0006 % <dim> was the dimension of <m> that was used in reshape2D 0007 % <msize> was the size of <m> 0008 % 0009 % return <f> but with the same dimensions as passed to reshape2D. 0010 % 0011 % example: 0012 % a = randn(3,4,5); 0013 % b = reshape2D(a,2); 0014 % isequal(size(b),[4 15]) 0015 % c = reshape2D_undo(b,2,size(a)); 0016 % isequal(size(c),[3 4 5]) 0017 % isequal(a,c) 0018 0019 % figure out the permutation order that was used in reshape2D 0020 dimorder = [dim setdiff(1:max(length(msize),dim),dim)]; 0021 0022 % figure out the unsquished size 0023 if dim > length(msize) % if weird case (the dimension that was shifted was off the deep end), then handle directly 0024 reshapesize = [1 msize]; 0025 else % otherwise, handle normally 0026 reshapesize = msize(dimorder); 0027 end 0028 0029 % unsquish and the permute back to the original order 0030 m = ipermute(reshape(f,reshapesize),dimorder);