Convey binary sting into 2 bits format

1 回表示 (過去 30 日間)
Siva
Siva 2024 年 10 月 7 日
編集済み: Walter Roberson 2024 年 10 月 9 日

I had an binay values file in matrix format . Eg [101101110111010, 111111111111111,010011110101111] . I want to seperate 15bits each into 2 bits and write it in other file in decimal format

  5 件のコメント
Steven Lord
Steven Lord 2024 年 10 月 9 日
You haven't said how this data is stored in MATLAB. The answer will be different if you have it stored as a vector containing just 0 and 1 values, a vector containing false and true values, a vector containing the characters '0' and/or '1', an integer value where you expect to perform bitwise operations to decompose it into individual bits, a double precision value (in which case what are the 16 bits that you're not showing us supposed to represent -- a double is stored as 64 bits), or something else entirely.
I'm going to assume the 2 in your example was just a typo.
DGM
DGM 2024 年 10 月 9 日
We also don't really know what the output is either.
Are we to assume it's just a text file? If it's supposed to be "decimal format", does that mean it's all a bunch of integers from 0 to 3?
xdec = 25831; % decimal
xbin = dec2bin(xdec,16); % char representation of binary
xbin = reshape(xbin,2,[]).'; % reshaped
xout = bin2dec(xbin).' % each 2b chunk is converted to decimal?
xout = 1×8
1 2 1 0 3 2 1 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
... or does that mean that the output is a bunch of decimal 0s and 1s in a file?
  1. How are the numbers represented in MATLAB?
  2. Are the numbers odd width? If yes, to what width should they be padded? 16? 48?
  3. Are these signed or unsigned values?
  4. Should they be converted into "decimal format" as shown?
  5. How is the output file to be formatted? CSV? Text? Something else? Delimiters? Be specific.
This is your question. It's up to you to say what you want.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 10 月 9 日

11111111111111111111110000011120101111000111100000 (48 bytes)

MATLAB is going to treat this as a decimal number, in double precision. Unfortunately double precision numbers only get up to roughly 10^15 (15 digits) before they start to lose precision. Only roughly the first 15 digits will be preserved ,and the rest will be the closest representable number.

There is no way around this problem as long as you insist on double precision representation.

If you were to instead code

0b11111111111111111111110000011110101111000111100000

then matlab would know to use a binary representation, in this case a uint64 number. The limit however would be 63 or 64 bits of data.

  1 件のコメント
Walter Roberson
Walter Roberson 2024 年 10 月 9 日
編集済み: Walter Roberson 2024 年 10 月 9 日
In particular,
T = 11111111111111111111110000011110101111000111100000
T = 1.1111e+49
fprintf("%.999g\n", T)
11111111111111110805019569335803527359330256945152
T1 = 0b11111111111111111111110000011110101111000111100000
T1 = uint64 1125899646464480
fprintf("%.999g\n", T1)
1125899646464480
dec2bin(T1)
ans = '11111111111111111111110000011110101111000111100000'

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStandard File Formats についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by