function x=meanlastdims(x,n)
% meanlastdims - compute average over last n dimensions of multidim array
%
% Usage: y=meanlastdims(x,n)
%
% If size1(x) = [m(1) m(2) ... m(k)], then size1(y) = [m(1) ... m(k-n)]
% MEANLASTDIMS ignores any trailing singleton dimensions including 2nd
% dimension of a column vector.

sz=size1(x);
for d=length(sz)-(0:n-1);
	x=sum(x,d)/sz(d);
end
