0
$\begingroup$

How can I use ‘randi’ function in MATLAB to generate a signal sequence s(t) which contains 1e6 samples which are either 0 or 1 and are equally probable. Any suggestions? Thanks!

$\endgroup$
3
  • 1
    $\begingroup$ Hint: if you have integers in the range X to Y, how can you convert them to numbers in the range X+Z to Y+Z? $\endgroup$
    – MBaz
    Apr 17, 2021 at 16:07
  • $\begingroup$ For a binary sequence you can also use "logicals" instead of integer type. x = rand(n,1) > 0.5; would do it. $\endgroup$
    – Hilmar
    Apr 17, 2021 at 16:29
  • 1
    $\begingroup$ The answer is randi([0, 1], numSamples, 1);. $\endgroup$
    – Royi
    Apr 24, 2021 at 11:52

1 Answer 1

2
$\begingroup$

Generate random integer uniformly distributed between 1 or 2. Then you can shift it to your liking.

X=randi(2,size,1)-1

$\endgroup$
5
  • $\begingroup$ Thanks for the answer! So I replace size with 1e6 and that would generate equally probable samples between 1 and 2? So, what about 0 and 1? $\endgroup$ Apr 17, 2021 at 16:32
  • 1
    $\begingroup$ Thats what the -1 for (: $\endgroup$
    – Ran Greidi
    Apr 17, 2021 at 16:33
  • $\begingroup$ Uhh Shoot missed that completely! I see! Thanks a lot for the help! $\endgroup$ Apr 17, 2021 at 16:34
  • 1
    $\begingroup$ At first you generate a vector of your size of ones and twos then you shift it $\endgroup$
    – Ran Greidi
    Apr 17, 2021 at 16:34
  • 1
    $\begingroup$ You could do it much simpler (Directly) by randi([0, 1], numSamples, 1);. $\endgroup$
    – Royi
    Apr 24, 2021 at 11:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.