function minu = minfiltnd(u, S)
	%% minu = minfiltnd(u, S)
	% applies a sliding minimum filter to the Nd array u(i, ...) 
	% with the rectangular/cuboidal etc. kernel specified by S
	% 
	% if u is of dimension N, then S must be a 1xN or Nx1 vector
	% no dimension of u may be smaller than the corresponding window size in
	% S. S may only ever contain odd integers greater than or equal to 3.
	%
	% formally, the filter implements this:
	%
	% minu(i, j, k ... ) = min(u(x, y, z ...))
	% where the minimum is taken over:
	%     i - (S(1)-1)/2 <= x <= i + (S(1)-1)/2
	%     j - (S(2)-1)/2 <= y <= j + (S(2)-1)/2
	% etc.
	
	%% check the inputs are valid
	Ndims		= ndims(u);
	if numel(u) == size(u,1)
		Ndims = 1;
	end	
	if(Ndims ~= length(S))
		error('u must have the same number of dimensions as the vector S has elements');
	end
	if length(S) ~= numel(S)
		error('S must be a vector');
	end
	if any(mod(S, 2) ~= 1) || any(S < 3)
		error('The window size specified by S must always be an odd integer greater than or equal to 3');
	end
	if any(size(u) < S)
		error('The window size in the nth dimension of u may not exceed the size of u in the nth dimension');
	end
	
	%% apply S consecutively along each dimension
	minu		= minfilt1d(u, S(1));
	clear u;
	for dim = 2:Ndims
		minu = minfilt1d(minu, S(dim), dim);
	end
end