How to find all possible combinations of balls? [duplicate]
问题 This question already has answers here : Generate a matrix containing all combinations of elements taken from n vectors (4 answers) Closed last month . I need to find all possible ways to combine k amount of balls from n types of balls. Let's say if there are 3 types of balls and I want to take 2, result I want is: 1 1 1 2 1 3 2 2 2 3 3 3 I am trying to do it with the following line: unique(nchoosek(repmat(1:n, 1, n), k), 'rows') I get: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 How to do find all