If you look inside ndims.m, you can see
Put simply, it is LENGTH(SIZE(X)).
Now size always returns a vector of length >=2, even for empty arrays (i.e. size([]) is [0 0]). Why that is? Likely a design choice made by TMW long ago.
If you want to measure "actual dimensions" you might want to use:
function nad = nActDims(in)
if numel(in) == 1
nad = 1;
else
nad = sum(size(in)>1);
end
or some combination of the functions isscalar, isvector, ismatrix.
That's just the way size is written.
If you wanted a one-liner, you can use subsref to index the one-output form of size:
out = someFunction(arg1,arg2,...
subsref(size(A),struct('type','()','subs',{{[2,3]}})));
And if you're going to be doing this a lot, add a function somewhere on the Matlab path or make an line one:
sizes = @(A,dims) subsref(size(A),struct('type','()','subs',{{dims}}));
out = someFunction(arg1,arg2,sizes(A,[2,3]));
You can also create sizes without a direct call to subsref by a little indirection with function handles:
getSizes = @(d,s) d(s);
sizes = @(A,s) getSizes(size(A),s);
which may be clearer and more maintainable.
Both Troy Haskin's answers and mine are borrowed from this question: How can I index a MATLAB array returned by a function without first assigning it to a local variable? I personally find the getfield approach appropriate for your case, where you just wrap getfield around your size function:
A = randn(1,2,3,4,5); %// 5D double
out = getfield(size(A),{[2 3]})
out =
2 3
Using subsref is probably the better approach as more direct and faster, but it could make your code less readable, as it is very specific hack.