登录论坛

查看完整版本 : 整数数组到二进制数组


poster
2019-12-10, 20:41
我有一个整数数组:

a=[3,4,5,6,7]; 我想将其转换为每个四位的二进制数组。对于上面的整数数组,我想获取以下二进制数组:

abinary=[0,0,1,1, 0,1,0,0, 0,1,0,1, 0,1,1,0, 0,1,1,1]; 有什么快速的方法吗?



回答:

Matlab具有内置功能DEC2BIN (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dec2bin.html) 。它创建了一个字符数组,但是很容易将其转换为数字。

%# create binary string - the 4 forces at least 4 bits bstr = dec2bin([3,4,5,6,7],4) %# convert back to numbers (reshape so that zeros are preserved) out = str2num(reshape(bstr',[],1))'

更多&回答... (https://stackoverflow.com/questions/2687678)