binary matrix that has different orders of 0 and 1

3 ビュー (過去 30 日間)
mo
mo 2015 年 1 月 1 日
編集済み: Shoaibur Rahman 2015 年 1 月 1 日
I would like to make a binary matrix such that each row has different order of 0 and 1.
For instance if the matrix has 2 columns then it would be 2^2 aspects (4 rows) then the matrix should be as below:
[1 1;
0 1;
1 0;
0 0]
Then if there are 3 columns, there will be 2^3 (=8) rows, and each row should have different orders of 0 and 1 like below:
[1 1 1;
0 0 1;
1 0 0;
0 1 0;
1 1 0;
1 0 1;
0 1 1;
0 0 0]
Can anybody help me to make this matrix?
  1 件のコメント
Roger Stafford
Roger Stafford 2015 年 1 月 1 日
編集済み: Roger Stafford 2015 年 1 月 1 日
It isn't clear how the ordering of your rows generalizes for an arbitrary power of 2. Please explain its logic or give many more examples. If the ordering is to be random, please say so.

サインインしてコメントする。

回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 1 月 1 日
編集済み: Azzi Abdelmalek 2015 年 1 月 1 日
n=3
out=dec2bin(0:2^n-1,n)
%or
out=dec2bin(0:2^n-1,n)-'0'
  1 件のコメント
Image Analyst
Image Analyst 2015 年 1 月 1 日
This is a character array and he wants binary, which might mean logical data type, or maybe integer or even double. Can you show how to make those other data types (logical, int32, double) from these strings?

サインインしてコメントする。


Shoaibur Rahman
Shoaibur Rahman 2015 年 1 月 1 日
編集済み: Shoaibur Rahman 2015 年 1 月 1 日
If the ordering of rows is not a concern, then use:
n = 3;
A = dec2bin(0:2^n-1);
B = double(A)-48
Note: 48 is the char to double conversion base for 0.

Pourya Alinezhad
Pourya Alinezhad 2015 年 1 月 1 日
hi, you can easily construct the matrix using:
A=zeros(size^2,size)
where "size" is column number of the matrix. then in order to fill it you can use "Gray" codes. google "Gray code" for more info.
  1 件のコメント
Roger Stafford
Roger Stafford 2015 年 1 月 1 日
That isn't gray code. Gray code changes only one bit with each step, and in Mo's three-column example, that isn't the case.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by