如何来填充所有的组合的二进制矩阵? [英] How to populate binary matrices with all the combinations?
问题描述
我想与所有的0和1它们组合2 ^ N矩阵。例如,对于N = 6(N =#行×#columns)阵{1} = [0 0 0; 0 0 0]
,阵{2} = [0 0 0; 0 0 1]
... 数组{64} = [1 1 1 1 1 1]
。我使用MATLAB和我碰到与 combn.m
(M = COMBN(V,N)返回矢量五元素N个元素的所有组合M具有尺寸(长(V)。^ N)×-N。) DEC2BIN()
,但我不能让它完全正确。我的另一个想法是建立一个大型矩阵,然后将其拆分为2 ^ N矩阵。例如,对于N = 6(2×3),我这样做 M = combn([0 1],3)
这给了我:
I want to have 2^n matrices with all the combinations of 0 and 1 in them. For example, for n=6 (n=#rows x #columns) array{1}=[0 0 0; 0 0 0]
,array{2}=[0 0 0; 0 0 1]
... array{64}=[1 1 1;1 1 1]
. I am using MATLAB and I came across with combn.m
(M = COMBN(V,N) returns all combinations of N elements of the elements in vector V. M has the size (length(V).^N)-by-N.), dec2bin()
but I can't get it quite right. Another idea of mine was to create a large matrix and then split it into 2^n matrices. For instance,for n=6( 2 x 3), i did this M=combn([0 1],3)
which gives me:
M =
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
然后,使用该 M
来创建这样一个更大的矩阵 M2 = combn(M 2)
,但是这将产生错误的结果。不过,如果我串联M个行是这样的:
Then, use this M
to create a larger matrix like this M2=combn(M,2)
, but this produces the wrong results. However, if i concatenate M row like this:
M = combn([000; 010; 100; 001; 110; 011; 101; 111],2)
我得到的东西更接近我的期望,即
M=combn([000;010;100;001;110;011;101;111],2)'
I get something closer to what I expect i.e
M =
Columns 1 through 21
0 0 0 0 0 0 0 0 10 10 10 10 10 10 10 10 100 100 100 100 100
0 10 100 1 110 11 101 111 0 10 100 1 110 11 101 111 0 10 100 1 110
Columns 22 through 42
100 100 100 1 1 1 1 1 1 1 1 110 110 110 110 110 110 110 110 11 11
11 101 111 0 10 100 1 110 11 101 111 0 10 100 1 110 11 101 111 0 10
Columns 43 through 63
11 11 11 11 11 11 101 101 101 101 101 101 101 101 111 111 111 111 111 111 111
100 1 110 11 101 111 0 10 100 1 110 11 101 111 0 10 100 1 110 11 101
Column 64
111
111
在那里我能得到的每一列,并分别将其转换成64 matrices.So,例如第1列将被从转换[0; 0]
到 [0 0 0; 0 0 0]
等。但是,我相信这是它可以在更短的时间内解决,优雅的一个更容易的问题。
where I can get each column and convert it separately into 64 matrices.So, for example column 1 would be converted from [0;0]
to [0 0 0;0 0 0]
etc. However, i believe it is a much easier problem which it can be solved in less time, elegantly.
推荐答案
使用的 DEC2BIN
:
r = 2; %// nunber of rows
c = 3; %// number of columns
M = dec2bin(0:2^(r*c)-1)-'0'; %// Or: M = de2bi(0:2^(r*c)-1);
M = reshape(M.',r,c,[]);
M
是大小的三维阵列研究
X C
X 2 ^(R * C)
,这样 M(:,:,1)
是第一矩阵 M(:,:,2)。
是第二等
M
is a 3D-array of size r
x c
x 2^(r*c)
, such that M(:,:,1)
is the first matrix, M(:,:,2)
is the second etc.
的它是如何工作的:
DEC2BIN
给出了一个二进制字符串重新一批presentation。因此, DEC2BIN(0:2 ^(R * C)-1)
给出了从 0
所有号码 2 ^(R * C)-1
前$ p $二进制pssed,每一个行。在 - '0'
部分刚刚转串入 0
的数值向量和 1
值。然后重塑
会在每个那些行成研究
X C
的形式,弥补了每个所需的矩阵。
dec2bin
gives a binary string representation of a number. So dec2bin(0:2^(r*c)-1)
gives all numbers from 0
to 2^(r*c)-1
expressed in binary, each in one row. The -'0'
part just turns the string into a numeric vector of 0
and 1
values. Then reshape
puts each of those rows into a r
x c
form, to make up each of the the desired matrices.
这篇关于如何来填充所有的组合的二进制矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!