generating 2 bits sequence of binary data

34 次查看(过去 30 天)
tag_arr = randi(0:1,1,5);
is giving random 0 and 1
tag_arr = randi(0:1,1,5)
tag_arr =
0 0 0 1 1
I am trying to gain 2 bits data
that means tag_arr should be randomly show in the following manner
tag_arr =
00 10 01 10 01 10
i tried several ways of using randi syntax but its not working kindly help
  3 个评论
Ayesha Punjabi
Ayesha Punjabi 2019-2-27
yes but I am trying only to have binary characters.
like I am trying to write syntax that will give me random 1 and 0 but in two or three bit format
like
10 10 10 01 00 11
or 110 001 101 010
Walter Roberson
Walter Roberson 2019-2-27
"binary characters" do not exist in MATLAB. Signed and unsigned integers exist in MATLAB; single precision and double precision floating point value exist in MATLAB; characters exist in MATLAB; logical values (that are mostly treated as being 0 and 1 for numeric purposes) exist in MATLAB. General binary does not exist in MATLAB. The closest MATLAB gets is the fixed-point datatype.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2019-2-27
编辑:Walter Roberson 2019-2-27
You cannot do that. MATLAB does not have a "binary" datatype that can display a bit pair without a space between the bits.
Your choices are:
  • use decimal instead of binary. So 00 10 01 10 01 10 would be decimal zero ten one ten one ten and would display as 0 10 1 10 1 10 . The decimal values would have to be broken up again using mod() or similar if you wanted to do computation.
  • use arrays of numbers. So 00 10 01 10 01 10 would be [0 0; 1 0; 0 1; 1 0; 0 1; 1 0] . This would be randi(0:1, 5, 2) . Nothing special would need to be done to use computation.
  • Use character arrays. So 00 10 01 10 01 10 would be ['00'; '10'; '01'; '10'; '01'; '10']. This would be char(randi(0:1, 5, 2)+'0') . The character arrays would have to have '0' (character for digit 0) to be subtracted to do computation.
  • Use character vectors. So 00 10 01 10 01 10 would be '00 10 01 10 01 10' with embedded spaces. This is a bit more of a nuisance to generate. The character vector would have to be transformed in order to do computation.
  • Use decimal values internally and transform them for display. So 00 10 01 10 01 10 would be stored as decimal 0 2 1 2 1 2 and you would use a small function to display them in binary when you wanted to see them.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by