MATLAB: I have 6 numbers with repetation : 1,2,2,3,3,4. I want all possible combinations using any 4 of the numbers. How will I get that

all possible combinations with repetationscombinationcombinations if there are one or more repeatitng elementspermutation

Generally I use 'nchoosek' function if there is no repetation. Example: A=1:6; combinations=nchoosek(A,4); combinations

Best Answer

You can still use nchoosek. Just use it to create indices into the vector rather than the vector itself:
v = [1,2,2,3,3,4];
Idx = nchoosek(1:size(v,2),4);
Out = v(Idx)
Out =
1 2 2 3
1 2 2 3
1 2 2 4
1 2 3 3
1 2 3 4
1 2 3 4
1 2 3 3
1 2 3 4
1 2 3 4
1 3 3 4
2 2 3 3
2 2 3 4
2 2 3 4
2 3 3 4
2 3 3 4
Note the difference between ‘Idx’ and ‘’Out.