Convert Binary matrix to Double Value

39 次查看(过去 30 天)
Hello, I want to convert an array stored with binary values into a double value which has those binary values but in one number. For example, A=[1 0 0 1 0 1] to B=[100101]. What is the logic behind this?

回答(2 个)

Guillaume
Guillaume 2019-11-27
Bearing in mind that for A = [0 0 0 0 0 0] B is just 0 and that above 16 bits you don't have enough precision in a double number:
B = str2double(char(A + '0'))
is one way.
B = polyval(A, 10)
is another.
  2 个评论
Elliot Alderson
Elliot Alderson 2019-11-30
The second method works, but I have a 16 bit binary stream, and it converts the rest into powers of 10 such as:
zero_bin= polyval( (de2bi(('0'-0),16)), 10);
gives me
zero_bin =
1.100000000000000e+11
This isn't the correct answer to my problem.
Guillaume
Guillaume 2019-11-30
Both methods work and both give the correct answer (for up to 16 bits).
Matlab always display double of this magnitude in engineering notation. There's no option to change that, but the number is stored correctly (again for numbers up to 16 bits). You can see the actually value with
fprintf('%d\n', B)
You've asked for the number to be stored as double, so that's what I've given you. Of course storing it as double is in my opinion silly, you're using 64 bits (doubles are 64 bits) to store 16 bits. Furthermore, as I keep saying, storing binary numbers as digits of a decimal number only work up to 16 bits. Above that, double doesn't have enough precision to store the numbers properly.
You could instead store the number as uint64. That would fix your display issue and in theory work up to 19 bits.
B = uint64(polyval(A, 10));
%or
B = uint64(str2double(char(A + '0'));
In practice, you're still limited to 16 bits. And you're still using 64 bits to store 16 bits.
You could also store the number as a char vector as suggested by David,
B = char(A + '0');
This allows you to store as many bits as you want, but use 16 bits to store each bit (so 256 bits to store 16 bits!).
The other option is of course to store the number as a 16 bit integer, which just use 16 bits
B = bin2dec(char(A + '0'), 'uint16')

请先登录,再进行评论。


David Hill
David Hill 2019-11-27
B=num2str(A);
B=B(B~=' ');%binary in matlab is a string
  3 个评论
David Hill
David Hill 2019-11-27
Thanks, that helped clear up a misunderstanding. Is the only way to display the actual binary by string (dec2bin)? Is there a way to keep the format in 0xb while doing computations? Or keep the format in hex while doing computations? I have never been able to figure out how to do computations while keeping the display format in binary or hex. I have always had to convert my decimal answer to display binary or hex at the conclusion of the computation (that was my confusion).
Guillaume
Guillaume 2019-11-30
You can use format hex to display all numbers (including floating point) as hex. Otherwise, yes, you've got to use dec2bin or dec2hex, or fprintf.

请先登录,再进行评论。

类别

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