整数数组为二进制数组 [英] Integer array to binary array

查看:137
本文介绍了整数数组为二进制数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数数组:

a=[3,4,5,6,7];

我想将它与每个四比特转换为二进制数组。对于上述整数数组我希望得到以下二进制数组:

I want to convert it to a binary array with four bits each. For the above integer array I would like get the following binary array:

abinary=[0,0,1,1, 0,1,0,0, 0,1,0,1, 0,1,1,0, 0,1,1,1];

有没有快速的方法来做到这一点?

Is there any fast way to do it?

推荐答案

Matlab具有内置功能的 DEC2BIN 。它创建了一个字符数组,但它很容易把这一回数字。

Matlab has the built-in function DEC2BIN. It creates a character array, but it's easy to turn that back to numbers.

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

这篇关于整数数组为二进制数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆