Thank you to David and Jonas for providing those answers! I really do appreciate the help. I tried both and they worked!
Producing all combinations of a vector with replacement
조회 수: 78 (최근 30일)
이전 댓글 표시
May_the_degrees_of_freedom_be_with_you
2021년 7월 13일
답변: Alec Jacobson
2023년 6월 3일
I am trying to produce all unique combinations of a vector with replacement, given some "choose" parameter, but neither the nchoosek() nor the perms() functions do exactly what I am trying to do.
For example, all unique combinations of the integers 1 and 2, derived manually, are:
uc = [1 1; 1 2; 2 2]
uc =
1 1
1 2
2 2
Similarly, all unique combinations of the integers 1, 2, and 3, choosing only two in any permutation are:
uc2 = [1 1; 1 2; 1 3; 2 2; 2 3; 3 3]
uc2 =
1 1
1 2
1 3
2 2
2 3
3 3
The C output for nchoosek() does not produce the desired result because it returns all possible combinations without replacement. For example:
L2 = [1 2];
L2k2 = nchoosek(L2,2)
L2k2 =
1 2
L3 = [1 2 3];
L3k2 = nchoosek(L3,2)
L3k2 =
1 2
1 3
2 3
The perms() function also does not work for this purpose because it both samples without replacement and produces duplicate combinations. For example:
test1 = perms(L2)
test2 = perms(L3)
test1 =
2 1
1 2
test2 =
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
Is there a way to toggle the settings of the nchoosek() function so that it produces all possible combinations of a vector v, given k values in each row, with replacement?
Thank you in advance.
채택된 답변
추가 답변 (3개)
Jonas
2021년 7월 13일
you could e.g. use
nchoosek(repelem(1:3,2),2)
but then you whould have to use unique(...,'rows') on the resulting combinations
댓글 수: 0
Alec Jacobson
2023년 6월 3일
https://stackoverflow.com/questions/28284671/generating-all-combinations-with-repetition-using-matlab/28284672#28284672
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Math에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!