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

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

问题描述

我有一个整数数组:

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];

有什么快速的方法吗?

推荐答案

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天全站免登陆