Converting 0 to -1, not working in function

Hello,
I am working on a simple indexing problem where I have an array of random binary data. I simply want to convert the 0's to -1's and I am using the following code to do that:
if true
bpsk = signal;
bpsk( signal==0 )= -1;
end
where signal is my binary data. So this works fine in the command window but when I stick this code into a function bpsk is all 1's after the indexing. Any thoughts?
Thanks, Micah

 Accepted Answer

signal = rand(1,10)>0.5
signal(signal==0) = -1
If signal is a logical, it can ONLY have values of 0 or 1, so anything that is nonzero will be 1. Perhaps you want to convert signal to double first?
signal = rand(1,10)>0.5
signal = double(signal)
signal(signal==0) = -1

More Answers (0)

Community Treasure Hunt

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

Start Hunting!