%linear indexing order of s=[i_1 i_2 ... i_m],
%where m>=2 and each index i_k runs from 1 through n with n>=2
%r is optional unless reversed linear indexing is needed
%default r=1 for linear indexing
%r=-1 for reversed linear indexing
function ind=lind(s,r)
if nargin<2
    r=1;
end
n=s(1);
m=length(s);
N=n^m;
ind=zeros(N,m);
indi=cell(1,m);
for i=1:N
    if r==1
        [indi{:}]=ind2sub(s,i);
        ind(i,:)=[indi{:}];
    elseif r==-1
        [indi{:}]=ind2sub(s,i);
        ind(i,:)=fliplr([indi{:}]);
    end
end
