function B=revdims(A,N)
% revdims - reverse N dimensions of a multidimensional array
%
% revdims :: [[D:[[1,M]]]], N:natural -> [[D2:[[1,M]]]].
% revdims :: [[D:[[1,M]]]] -> [[D2:[[1,M]]]].
%
% This takes an array of size [D(1:N) DX]
% and returns an array of size [D(N:-1:1) DX]
% where DX are the rest of the dimensions after the Nth and are
% unaffected. N defaults to the number of dimensions disregarding
% trailing singletons, ie numdims(A)==length(size1(A))

if nargin<2, N=numdims(A); end

ND=max(N,2);
DD=1:ND;
DD(1:N)=fliplr(DD(1:N));
B=permute(A,DD);
