|
"Raghavasimhan Thirunarayanan" <kingmakerbull@rediffmail.com> wrote in message <gm9qv9$qf4$1@fred.mathworks.com>...
> Hello,
> I have been trying to do some bit splitting with matlab but could not do so. I want to take a binary number say 01001 and convert it into a vector by the 0 in the binary number with 1 and 1 in the binary number with -1. FOr example, 01001 would yield the vector [1 -1 1 1 -1] where the 0 has been replaced by '1' and 1 in the binary replaced with '-1'. I want to place thios function in a loop so that I can continuously increment the binary number and generate the vector. Kindly help me in this case.
>
> Thanks
Probably not the most elegant solution, but here's one way:
num_dec = 9; % this is your 01001
num_bin = dec2bin(num_dec);
result = str2double(strrep(strrep(cellstr(num_bin')','1','-1'),'0','1')),
ans =
-1 1 1 -1
If you're starting with num_bin equal to the string '01001' directly, then the above yields
ans =
1 -1 1 1 -1
HTH,
/HJ
|