function ndx = sub2indvec(N,subinds)
% Computes the position of kernel element
% 
%SUB2INDVEC(N,subinds) computes the position of the kernel element
%associated with the multi-index subinds inside a vectorized non-redundant
%kernel having memory N.
%
%  Input arguments:
%
%  subinds : multi-index vector with integer components in {1,...,N}
%  N : memory of the kernel
%
%   CBVK Toolbox
%   JHMG, PMSB 2016

p = size(subinds,2);

if p==1
    ndx = subinds;
    return;
end

ndx = zeros(size(subinds,1),1);

for r = 1:size(subinds,1)
    
    s0 = 1;
    
    for q = 1:p-1
        
        pq = p - q;
        
        for s=s0+1:subinds(r,q)
            Nq = N - s + 2;
            ndx(r) = ndx(r) + nchoosek(Nq + pq - 1, pq);
        end
                                
        s0 = subinds(r,q); 
    end;
    
    ndx(r) = ndx(r) + 1 + subinds(r,end) - s0;
end