Matlab二进制编码
我有一个包含一系列整数的向量,我要做的是取所有的数字,将它们转换成相应的二进制形式,并将所有结果的二进制值连接在一起。有什么简单的方法可以做到这一点吗 e、 g.a=[1 2 3 4]->b=[0000000 1 000000 10 000000 11 00000100]->c=0000000 100000000000 100000011000000是,使用,然后是字符串连接。尝试:Matlab二进制编码,matlab,encoding,binary,Matlab,Encoding,Binary,我有一个包含一系列整数的向量,我要做的是取所有的数字,将它们转换成相应的二进制形式,并将所有结果的二进制值连接在一起。有什么简单的方法可以做到这一点吗 e、 g.a=[1 2 3 4]->b=[0000000 1 000000 10 000000 11 00000100]->c=0000000 100000000000 100000011000000是,使用,然后是字符串连接。尝试: b = dec2bin(a) 正如其他答案所指出的,函数是解决此问题的一个选项。但是,正如所指出的,在转换大量
b = dec2bin(a)
正如其他答案所指出的,函数是解决此问题的一个选项。但是,正如所指出的,在转换大量值时,这可能是一个非常缓慢的选项 要获得更快的解决方案,您可以使用以下功能:
a = [1 2 3 4]; %# Your array of values
nBits = 8; %# The number of bits to get for each value
nValues = numel(a); %# The number of values in a
c = zeros(1,nValues*nBits); %# Initialize c to an array of zeroes
for iBit = 1:nBits %# Loop over the bits
c(iBit:nBits:end) = bitget(a,nBits-iBit+1); %# Get the bit values
end
c = char(c+48);
结果c
将是一个0和1的数组。如果要将其转换为字符串,可以使用以下函数:
a = [1 2 3 4]; %# Your array of values
nBits = 8; %# The number of bits to get for each value
nValues = numel(a); %# The number of values in a
c = zeros(1,nValues*nBits); %# Initialize c to an array of zeroes
for iBit = 1:nBits %# Loop over the bits
c(iBit:nBits:end) = bitget(a,nBits-iBit+1); %# Get the bit values
end
c = char(c+48);