function index=index(k,n_grid,dim) index=zeros(1,dim); for i=dim:-1:1 index(i)=floor(k/n_grid^i); k=k-index(i)*n_grid^i; end index= end