function sub = ind2subv_noC(siz, ndx)
% IND2SUBV Like the built-in ind2sub, but returns the answer as a vector.
% sub = ind2subv(siz, ndx)
%
% e.g., for sizes=[2 2 2],
% [1 1 1] = 1, [2 1 1] = 2, [1 2 1] = 3, ..., [2 2 2] = 8.
% That is, the leftmost digit toggle fastest.


if isempty(siz)
  sub = ndx;
  return;
end

siz = siz(:)';
n = length(siz);

if isequal(siz, twos(1,n))
  sub = reverse(dec2bitv(ndx-1, n))+1;
  return;
end

cp = [1 cumprod(siz)];
ndx = ndx - 1;
for i = n:-1:1,
  sub(i) = floor(ndx/cp(i))+1;
  ndx = rem(ndx,cp(i));
end
