How to convert a vector into bitstring?

4 views (last 30 days)
Anushka
Anushka on 29 Jun 2015
Commented: Walter Roberson on 29 Jun 2015
How to convert a vector into bitstring in matlab?

Answers (2)

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

Thorsten
Thorsten on 29 Jun 2015
Vector = [255 1 0];
reshape(dec2bin(Vector)', 1, [])
  1 Comment
Walter Roberson
Walter Roberson on 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.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!