how to write Binary table in simple way ?

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

 採用された回答

Walter Roberson
Walter Roberson 2015 年 9 月 12 日

0 投票

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 件のコメント

Henry Buck
Henry Buck 2015 年 9 月 12 日
編集済み: Henry Buck 2015 年 9 月 12 日
I want to print only the rows that have 2 bits equal to 1. in case 13 it has 3 bits equal to one......
Walter Roberson
Walter Roberson 2015 年 9 月 12 日
You no longer need to be able to print the complete table?
Does dec2bin(13) - '0' look like an example of a row you might want?
Henry Buck
Henry Buck 2015 年 9 月 12 日
編集済み: Walter Roberson 2015 年 9 月 12 日
no,
I want to print itlike thise one:
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
Walter Roberson
Walter Roberson 2015 年 9 月 12 日
Does dec2bin(5)-'0' look like an example of a row you might want? Does dec2bin(6)-'0' look like an example of a row you might want?
Henry Buck
Henry Buck 2015 年 9 月 12 日
編集済み: Walter Roberson 2015 年 9 月 13 日
yes,
dec2bin(5) = 0101
dec2bin(6) = 0110
for table of 4 bits - it is 16 possibilities.
only those:
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
I want to print/
Thanks,
Henry
Walter Roberson
Walter Roberson 2015 年 9 月 12 日
So if you had dec2bin(N)-'0' for some integer N, how would you test if two bits were set? See my hint about sum()
Henry Buck
Henry Buck 2015 年 9 月 13 日
編集済み: Walter Roberson 2015 年 9 月 13 日
what wrong with this one to create the binary table ?
It seems that is does not work:
n=4;
for i=0:(2^n-1)
table= dec2bin(i,n)
end
Walter Roberson
Walter Roberson 2015 年 9 月 13 日
編集済み: Walter Roberson 2015 年 9 月 13 日
You are writing over "table" in each iteration of the loop.
n=4;
for i=0:(2^n-1)
table(i+1,:) = dec2bin(i,n)
end
Henry Buck
Henry Buck 2015 年 9 月 14 日
編集済み: Walter Roberson 2015 年 9 月 16 日
Hi,
This one does not work as I want.
The resilt start like this:
table =
0000
table =
0000
0001
table =
0000
0001
0010
And end like this:
table =
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
16 results.... for each one, a new line fromthat tble is added.
thanks,
Henry
Walter Roberson
Walter Roberson 2015 年 9 月 16 日
If you do not want the intermediate results printed out, put a semi-colon on the end of the assignment. You currently have
table(i+1,:) = dec2bin(i,n)
so change that to
table(i+1,:) = dec2bin(i,n);
Then you will only get the final result of 16 rows. When you have that, you can search it to find the rows that have 2 bits set.
Walid KESSAL
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
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
Pierre Bulens 2019 年 7 月 28 日

0 投票

dec2bin function gives the table :
binarytable.PNG
to get the rows containing 2 bits equal to 1, use the sum function and logical indexing
rows with 2 ones.PNG
the sum function gives the number of 1s in each row,
the '== 2' part selects the wanted rows

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by