How to generate all 2^n combinations of n choices from a 2-vector?

조회 수: 1 (최근 30일)
Jerry Guern
Jerry Guern 2025년 5월 31일
편집: Matt J 2025년 6월 2일
If I want all combinations of 3 choices of 0 or 1, I do this:
t = combinations([0 1],[0 1],[0 1]);
If I want all combinations of 4 choices, I have to do this:
t = combinations([0 1],[0 1],[0 1],[0,1])
If I want all combinations of 5 choices, I have to do this:
t = combinations([0 1],[0 1],[0 1],[0,1],[0 1])
...and so on. Is there any way to generate all combination of a variable number of choices?

채택된 답변

John D'Errico
John D'Errico 2025년 5월 31일
편집: John D'Errico 2025년 5월 31일
Simple enough. Don't use combinations.
n = 3;
t = dec2bin(0:2^n - 1) - '0'
t = 8×3
0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Easy enough to make that into a table if you so desire.
Sigh. Do you INSIST on using combinations? This will suffice.
v = repmat({[0 1]},1,n);
t2 = combinations(v{:})
t2 = 8×3 table
Var1 Var2 Var3 ____ ____ ____ 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
I can think of at least a couple other ways if pushed, but they shoiuld work.
  댓글 수: 3
Steven Lord
Steven Lord 2025년 6월 2일
And of course, if you want a matrix from combinations (assuming all the table variables can be concatenated together) you can do that by indexing into the output from the function call.
n = 3;
t = dec2bin(0:2^n - 1) - '0';
v = repmat({[0 1]},1,n);
t2 = combinations(v{:}).Variables
t2 = 8×3
0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
doTheyMatch = isequal(t, t2)
doTheyMatch = logical
1
You need to be careful, though, if you have mixed types.
t3 = combinations([0 1], ["apple", "banana"])
t3 = 4×2 table
Var1 Var2 ____ ________ 0 "apple" 0 "banana" 1 "apple" 1 "banana"
t4 = t3.Variables % Note that this is a string array, using "0" and "1" not 0 and 1
t4 = 4×2 string array
"0" "apple" "0" "banana" "1" "apple" "1" "banana"
Matt J
Matt J 2025년 6월 2일
편집: Matt J 2025년 6월 2일
The spped performance as a function of n is interesting. combinations() starts to overtake ndgrid() only for n>=20 or so. I would have expected combinations() to benefit from avoiding a cat() operation.
Timing(15)
ans = 0.0017
ans = 8.8375e-04
ans = 6.7575e-04
Timing(20)
ans = 0.1078
ans = 0.0808
ans = 0.1195
function Timing(n)
timeit(@() methodDEC2BIN(n))
timeit(@() methodCOMBINATIONS(n))
timeit(@() methodNDGRID(n))
end
function methodDEC2BIN(n)
t = dec2bin(0:2^n - 1) - '0';
end
function methodCOMBINATIONS(n)
v = repmat({[0 1]},1,n);
t = combinations(v{:});
end
function methodNDGRID(n)
[t{n:-1:1}]=ndgrid([0,1]);
t=reshape( cat(n+1, t{:}) , [],n);
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2025년 5월 31일
편집: Matt J 2025년 5월 31일
n=3;
clear t
[t{n:-1:1}]=ndgrid([0,1]);
t=reshape( cat(n+1, t{:}) , [],n)
t = 8×3
0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by