How to convert a vector into bitstring?

20 Ansichten (letzte 30 Tage)
Anushka
Anushka am 29 Jun. 2015
Kommentiert: Walter Roberson am 29 Jun. 2015
How to convert a vector into bitstring in matlab?

Antworten (2)

Walter Roberson
Walter Roberson am 29 Jun. 2015
reshape(dec2bin(typecast(Vector(:),'uint8'),8) .', 1, [])

Thorsten
Thorsten am 29 Jun. 2015
Vector = [255 1 0];
reshape(dec2bin(Vector)', 1, [])
  1 Kommentar
Walter Roberson
Walter Roberson am 29 Jun. 2015
In general the length of that is not certain. dec2bin() will by default use the number of bits per row that is the number of bits required to represent max() of the data, so if all of your data happened to be in the range 0 to 63 (for example) then the result would be only 6 bits per entry. But your receiver doesn't usually know ahead of time what the restricted range will be so it is safer to force the use of the number of bits required for the maximum potential value.
Secondly this code assumes that the data is non-negative and that any floating point numbers should be rounded to the nearest integers. That might turn out to be a valid assumption for the purposes of the poster, but the more general case would require that floating point numbers be pulled apart for reconstruction in full precision on the other side; the code in my answer does that.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by