How to keep only 3 digits after the dot on a number?

3 vues (au cours des 30 derniers jours)
Nick
Nick le 6 Mai 2021
Commenté : Chunru le 11 Mai 2021
Hello
I have an array (1000x11) of binary numbers like this :
1 0 1 1 0 0 0 1 1 0 0
0 0 0 1 0 1 1 0 1 0 0
1 1 1 0 1 1 0 1 1 0 0
0 0 0 1 1 0 1 1 0 1 1
1 1 0 1 0 1 0 0 1 1 1
0 1 0 0 1 0 0 0 0 0 1
0 0 0 0 1 0 1 1 0 0 0
1 0 1 0 0 0 1 1 0 1 0
And I want to turn each row into a float decimal (x) with the last 10 binary digits as the numbers after the dots. I did this:
x = x(:, 1) + (x(:, 2:11) * 2.^(9:-1:0).') / 1000
but I got :
x = 1.8260
x =0.1110
x =1.9900
x =1.1640
x =0.3050
x = 0.6640
and I get the error 'Index exceeds matrix dimensions' so I have to take only the first 3 digits after the dot. How do I do that?
  3 commentaires
James Tursa
James Tursa le 7 Mai 2021
Modifié(e) : James Tursa le 7 Mai 2021
What would help us is for you to give us a description of inputs and desired outputs, with some small examples. E.g.,
"I am starting with these binary digits 101101001 and I want to do calculations that turn it into the floating point number ___."
(you fill in this blank for us)
Nick
Nick le 7 Mai 2021
Modifié(e) : Nick le 7 Mai 2021
"I am starting with these binary digits 10110001100 and I want to do calculations that turn it into the floating point number 1.396 ."
Could you also please check the files I attached? Maybe the mistake is there

Connectez-vous pour commenter.

Réponses (1)

Chunru
Chunru le 7 Mai 2021
Try the following:
x = randi([0 1], 4, 11)
%x = x(:, 1) + (x(:, 2:11) * 2.^(9:-1:0).') / 1000 ; % May not accurate
x = x * 2.^(0:-1:-10)' % This is the binary value
  5 commentaires
Jan
Jan le 8 Mai 2021
Then a binary representation an in Chunru's answer is better than a pseudo decimal interpretation.
Chunru
Chunru le 11 Mai 2021
When I said ""Not accurate", I meant the division by 1000 (rather 1024).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by