How to group and convert to decimal?

1 次查看(过去 30 天)
The output of r is 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1
I want to group every 8 digits and convert to decimal
Please help
  4 个评论
Image Analyst
Image Analyst 2013-2-4
编辑:Image Analyst 2013-2-4
You mean an "array" of doubles is returned by r(), like this [0, 0, 0, 1, 0, 1, 0, etc.], not just a single double like you implied. So, it's like what Azzi assumed.

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-2-4
x=[ 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1]
x=num2str(x)
x(x==' ')=[];
y=reshape(x,[],8);
out=bin2dec(y)
  3 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2013-2-4
Ok, there is an error in reshape function
x=[ 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1]
x=num2str(x)
x(x==' ')=[];
y=reshape(x,8,[])';
out=bin2dec(y)

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2013-2-4
编辑:Jan 2013-2-4
Converting from a double vector to a string and back to a double inside bin2dec is not efficient. Better convert from binary to decimal values directly:
x = [ 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1];
d = 2 .^ (length(x)-1:-1:0) * x(:);

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by