Matlab中的组合矩阵 [英] Combinations matrix in matlab
问题描述
此代码
combinations = dec2base(0:power(2,N*M)-1,2) - '0'
生成大小为N * M的零和一的所有可能组合,并将所有这些组合存储在称为组合的矩阵中.我需要知道它是如何工作的,因为我不理解此代码.谢谢
generates all possible combinations of zeros and ones for a matrix of size N*M and stores all these combinations in a matrix called combinations. I need to know how it works , because i don't understand this code . Thank you
推荐答案
以M = 2
,N = 3
为例.那么power(2,N*M)-1
是63
,0:power(2,N*M)-1
是向量[0 1 2 ... 63]
.
Consider M = 2
, N = 3
as an example. Then power(2,N*M)-1
is 63
, and 0:power(2,N*M)-1
is the vector [0 1 2 ... 63]
.
dec2base(..., 2)
使用字符'0'
和'1'
作为数字",将那些64
数字转换为基数2
.每个结果都排成一行,如果需要,用'0'
左填充.这样就给出了64
×6
char矩阵
dec2base(..., 2)
converts those 64
numbers into base 2
, using chars '0'
and '1'
as "digits". Each result is in a row, left-padded with '0'
's if needed. So it gives the 64
×6
char matrix
000000
000001
000010
....
111110
111111
要将这些字符转换为数字,请减去'0'
.这利用了字符'0'
和'1'
的ASCII码是连续的事实,给出了'0'
的0
和'1'
的1
.所以最终结果是数值矩阵
To convert those chars into numbers, subtract '0'
. That gives 0
for '0'
and 1
for '1'
, exploiting the fact that the ASCII codes for chars '0'
and '1'
are consecutive. So the final result is the numeric matrix
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 1 0
....
1 1 1 1 1 0
1 1 1 1 1 1
这篇关于Matlab中的组合矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!