필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Nick
Nick 2021년 5월 6일
댓글: Chunru 2021년 5월 11일
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
James Tursa
James Tursa 2021년 5월 7일
편집: James Tursa 2021년 5월 7일
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 2021년 5월 7일
편집: Nick 2021년 5월 7일
"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

댓글을 달려면 로그인하십시오.

답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by