Generate a random signal in Matlab

2 views (last 30 days)
I need to generate a transmit signal of 10,000 random bits in MATLAB with values of -1 or +1, with both having a equal probabilty of occuring and then verify the probability.
I am using y=randi([-1 1],10000)
But I keep getting 0's and do not know how to take them out and am unsure if my function is even correct.
I tried verifying the probabilty by doing 2 different matrices and adding them together and should be equal to 0, but bc i have zeros this did not work.
Any suggestions?

Accepted Answer

Daniel M
Daniel M on 17 Oct 2019
y = (rand(10000,1) > 0.5)*2 -1;
  8 Comments
Walter Roberson
Walter Roberson on 17 Oct 2019
p = sum(y==1)/numel(y);
for vector y that can be abbreviated to
p = mean(y==1)
for non-vector toss in a (:) after the y or use mean2() instead of mean()
Steven Lord
Steven Lord on 17 Oct 2019
for non-vector toss in a (:) after the y or use mean2() instead of mean()
You can specify 'all' as the dimension input to mean if you're using release R2018b or newer of MATLAB.
>> A = magic(5);
>> mean(A, 'all')
ans =
13
>> mean(A(:))
ans =
13
>> mean(A)
ans =
13 13 13 13 13

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation 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!