How to convert a vector into bitstring?

6 次查看(过去 30 天)
How to convert a vector into bitstring in matlab?

回答(2 个)

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

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by