Question: When doing permutation tests, we sometimes need to sample k out of n without replacement, which can be done by the function nchoosek(). However, the returned values only contain the indice for  group 1 but not for group 2. How can we get those observations in group 2?

 

Answer: Suppose that the combined sample is z=[z1 ... zn], and the size of group 1 is n1 and that of group 2 is n2. Of course, n1+n2=n. Let allperm = nchoosek(z,n1). Then allperm is a matrix with n1 rows and each row contains the indice of z elements forming group 1. Let perm1=allperm(1,:). To get the elements forming group 2, we do the following.

 

% group1

group1 = z(perm1)

% group 2

ind=zero(1,n);

ind(perm1)=1;

group2 = z(ind==0)