How to create all combinations of boolean vector possibilities?

조회 수: 10 (최근 30일)
Sophia Sperry
Sophia Sperry 2022년 2월 8일
댓글: Steven Lord 2022년 2월 8일
Some background for the problem - I have 5 situations that can happen independently of the other situations. I want to create a matrix with rows that look like [1 0 0 0 0] , [1 1 0 0 0], etc. where the rows will be 2^5-1 since the vector [0 0 0 0 0] doesn't matter. The boolean logic 1 means that the situation has occured, 0 means the situation has not occured. Is there a way to easily create this matrix? Please let me know if any further clarification is needed. Thank you for the help!

채택된 답변

David Hill
David Hill 2022년 2월 8일
편집: David Hill 2022년 2월 8일
x=1:2^5-1;
m=logical(dec2bin(x',5)-'0');
  댓글 수: 1
Steven Lord
Steven Lord 2022년 2월 8일
Instead of explicitly calling logical you could use the == operator.
x = 1:2^5-1;
m = dec2bin(x', 5) == '1'
m = 31×5 logical array
0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by