function ndx = NDsub2ind(siz,subs)
%function ndx = NDsub2ind(siz,subs)
%
%works more smoothly when the dimensionality of the mtrx is unknown

if((size(subs,1) ~= 1) && (size(subs,2) == 1))
   subs = subs';
end
siz = siz(:)';
if length(siz)<2
        error('MATLAB:sub2ind:InvalidSize',...
            'Size vector must have at least 2 elements.');
end

if ((length(siz) ~= size(subs,2)))
    error('NDsub2ind: length(siz) must = length(subs)');
end

nPoints = size(subs,1);


%Compute linear indices
k = [1 cumprod(siz(1:end-1))];
ndx = ones(nPoints,1);
s = size(subs); %For size comparison
for i = 1:length(siz),
    v = subs;
    if (any(v(:,i) < 1)) || (any(v(:,i) > siz(i)))
        %Verify subscripts are within range
        error('NDsub2ind:IndexOutOfRange','Out of range subscript.');
    end
    ndx = ndx + (v(:,i)-1)*k(i);
end
