Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Nick
Nick am 6 Mai 2021
Kommentiert: Chunru am 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 Kommentare
James Tursa
James Tursa am 7 Mai 2021
Bearbeitet: James Tursa am 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 am 7 Mai 2021
Bearbeitet: Nick am 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Chunru
Chunru am 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 Kommentare
Jan
Jan am 8 Mai 2021
Then a binary representation an in Chunru's answer is better than a pseudo decimal interpretation.
Chunru
Chunru am 11 Mai 2021
When I said ""Not accurate", I meant the division by 1000 (rather 1024).

Melden Sie sich an, um zu kommentieren.

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by