how to write Binary table in simple way ?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi there,
1. How to create Binary code (like this one below) in a simple way?
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
2. how to print only the relevant rows like:
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
only the rows that has 2 bits that equal to 1.....
Thanks,
Henry
댓글 수: 0
채택된 답변
Walter Roberson
2015년 9월 12일
What happens if you use dec2bin(13) - '0' ?
A way to calculate how many values are set in a row is to sum() the row.
You should also investigate logical indexing
댓글 수: 12
Walid KESSAL
2018년 3월 16일
Here's how you can take care of that problem Mr.Henry Buck.
n=4; tab=[];
for i=0:(2^n-1)
table(i+1,:) = dec2bin(i,n);
tab = [tab;table(i+1,:)];
end
table = tab
the result is this
Walter Roberson
2019년 7월 28일
That code is redundant: just assigning into table() is enough without needing to build that tab variable.
추가 답변 (1개)
Pierre Bulens
2019년 7월 28일
dec2bin function gives the table :
to get the rows containing 2 bits equal to 1, use the sum function and logical indexing
the sum function gives the number of 1s in each row,
the '== 2' part selects the wanted rows
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!