generating 2 bits sequence of binary data

18 Ansichten (letzte 30 Tage)
Ayesha Punjabi
Ayesha Punjabi am 20 Feb. 2019
Kommentiert: Walter Roberson am 27 Feb. 2019
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 Kommentare
Ayesha Punjabi
Ayesha Punjabi am 27 Feb. 2019
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 am 27 Feb. 2019
"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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 27 Feb. 2019
Bearbeitet: Walter Roberson am 27 Feb. 2019
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.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by