Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comments
James Tursa
James Tursa on 7 May 2021
Edited: James Tursa on 7 May 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 on 7 May 2021
Edited: Nick on 7 May 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

Sign in to comment.

Answers (1)

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

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!