MATLAB: Binary Number Transmitter

binary communcation

I need to create a transmitter that generates 100000 binary bits encoded as +/-3 volts using rand() but it doesn't seem to recognize the if just goes to the else. I know its something dumb any help would be appreciated I have:
x = rand(1,100000)
bit=(x>.5)
if bit==0
bit = bit-3
else x == 1
bit = bit+3
end

Best Answer

The "if" statement is only executed once, however you have 100000 elements. Essentially, if any elements in a vector are false, the "if" statement will return false. That's why you never enter the "true" portion of the if statement. Here's what I think you were trying:
x = rand(1,100000)
bit = double(x>.5);
bit(bit==0) = -3;
bit(bit==1) = 3;
Or, in a single line statement:
bit = (rand(1,100000)>0.5)*6 - 3;