フィルターのクリア

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 Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by