function ndx = subv2ind(siz, subv)
% SUBV2IND Like the built-in sub2ind, but the subscripts are given as a row vector.
% ind = subv2ind(siz,subv)
%
% e.g., ind = subv2ind([2 2 2], [1 1 1; 2 1 1; 1 2 1; ...; 2 2 2])
% ind = [1 2 ... 8] i.e., the leftmost digit toggles fastest.

if isvector(subv), subv = subv(:)'; end
nindices = size(subv, 1);
ndims = size(subv, 2);
assert(length(siz) == ndims);

k = [1 cumprod(siz(1:end-1))];
ndx = ones(nindices, 1);
for i = 1:ndims,
  ndx = ndx + (subv(:,i)-1)*k(i);
end
ndx = ndx';
