MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   整数数组到二进制数组 (https://www.labfans.com/bbs/showthread.php?t=23341)

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]; 有什么快速的方法吗?



[B]回答:[/B]

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

%# 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))'

[url=https://stackoverflow.com/questions/2687678]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 02:09

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.