Convert Binary matrix to Double Value

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?

Risposte (2)

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 Commenti

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.
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')

Accedi per commentare.

B=num2str(A);
B=B(B~=' ');%binary in matlab is a string

3 Commenti

"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;
%or
B = 0x65;
which are the most efficient way of storing binary numbers (i.e. as native numbers)
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).
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.

Accedi per commentare.

Categorie

Richiesto:

il 27 Nov 2019

Commentato:

il 30 Nov 2019

Community Treasure Hunt

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

Start Hunting!

Translated by