MATLAB Answers

Convert Binary matrix to Double Value

34 views (last 30 days)
Elliot Alderson
Elliot Alderson on 27 Nov 2019
Commented: Guillaume on 30 Nov 2019
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?


Sign in to comment.

Answers (2)

Guillaume on 27 Nov 2019
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.


Elliot Alderson
Elliot Alderson on 30 Nov 2019
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 =
This isn't the correct answer to my problem.
Guillaume on 30 Nov 2019
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));
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')

Sign in to comment.

David Hill
David Hill on 27 Nov 2019
B=B(B~=' ');%binary in matlab is a string


Guillaume on 27 Nov 2019
"binary in matlab is a string"
Huh? Says who?
Personally, I'd store that binary A = [1 0 0 0 1 0 1] as
B = uint8(69)
or if you're on R2019b,
B = 0xb1000101;
B = 0x65;
which are the most efficient way of storing binary numbers (i.e. as native numbers)
David Hill
David Hill on 27 Nov 2019
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 on 30 Nov 2019
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.

Sign in to comment.

Sign in to answer this question.