List of all Binomial permutations with a certain number of events

1 Ansicht (letzte 30 Tage)
Massimiliano Induberino
Massimiliano Induberino am 25 Nov. 2022
Kommentiert: Torsten am 28 Nov. 2022
Hello there,
I am trying to do something very simple I think and have come across this problem I cannot solve. I am not proficient with matlab.
All I want to do is to find all the possible permutation of tossing a coin 8 times where we ONLY have 4 heads and 4 tails. All of the possible combinations for only this scenario, the order does not matter.
so something like
1) H H H H T T T T
2) H H T T H H T T
3) H T T H H T H T
and so on....

Antworten (1)

Torsten
Torsten am 25 Nov. 2022
Bearbeitet: Torsten am 25 Nov. 2022
n = 8;
A = dec2bin(0:2^n-1)-'0'
A = 256×8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1
A4 = A(sum(A,2) == 4,:)
A4 = 70×8
0 0 0 0 1 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 1 0 1 1 0 0 0 1 1 1 0 1 0 0 0 1 1 1 1 0 0 0 1 0 0 1 1 1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 0 0 1 1 0 0 1 1
8*7*6*5/(4*3*2*1)
ans = 70
% more general
Ai = arrayfun(@(i)A(sum(A,2) == i,:),0:n,'UniformOutput',0)
Ai = 1×9 cell array
{[0 0 0 0 0 0 0 0]} {8×8 double} {28×8 double} {56×8 double} {70×8 double} {56×8 double} {28×8 double} {8×8 double} {[1 1 1 1 1 1 1 1]}
sum(arrayfun(@(i)size(Ai{i},1),1:n+1))
ans = 256
  2 Kommentare
Massimiliano Induberino
Massimiliano Induberino am 28 Nov. 2022
Hello there,
thank you very much for the answer!
I think what I was looking for is A4 since this only has the combinations where 4 Hs and 4 Ts are present.
A4 has all the combination where this can happen.
Is that correct?
Torsten
Torsten am 28 Nov. 2022
Yes.
The code after the comment
% more general
lists the combinations with 0,1,2,3,4,...,8 heads in a row separately.
The total number of combinations is 2^8, and this comes out to be true after the splitting of the possible outcomes.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by