function x=genMCE(p)
% Generate x from probs. p
[n,k]=size(p);

Z=cumsum(p,2);
r=rand(n,1);
x=zeros(1,n);
for i=k:-1:1,
    q=(r<=Z(:,i));
    x((q==1))=i;
end
