arrays - 整数数组转二进制数组

标签 arrays matlab binary

我有一个整数数组:

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 .它创建了一个字符数组,但很容易将其转换回数字。

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

关于arrays - 整数数组转二进制数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2687678/

相关文章:

c - 在方法内部初始化 char 指针

matlab - matlab如何获取函数参数

binary - Erlang io:将二进制格式格式化为十六进制

char[] 到 uint64_t

arrays - 带有 *ngFor 的 Angular2 奇怪的形式行为

javascript - 基于嵌套对象数组的过滤

c# - 使用 MongoDB C# 驱动程序 2.0 添加元素或添加到数组

matlab - 如何在 MATLAB 的 for 循环中调用函数?

matlab - 像在 Python 中一样在 Matlab 中交换元素

c - C 中从 16 位到 32 位的符号扩展