function iout=perms_mec(ni,niout)

% Enumerates permutations of niout elements in ni

nb=nchoosek(ni,niout);
iout=zeros(nb,niout);
i=1;
j=1;
id=1;

while i<=niout
	n=ni-id;
	k=niout-i;
	lgh=nchoosek(n,k);
	iout(j:j+lgh-1,i)=id;
	j=j+lgh;
	id=id+1;

	if n==k
		if j>nb
			i=i+1;
			j=1;
			id=i;
		else
			id=iout(j,i-1)+1;
		end
	end
end
	
endfunction