Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Bit splitting

Subject: Bit splitting

From: Raghavasimhan Thirunarayanan

Date: 3 Feb, 2009 16:22:01

Message: 1 of 3

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

Subject: Bit splitting

From: Heywood

Date: 3 Feb, 2009 16:43:02

Message: 2 of 3

"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

Subject: Bit splitting

From: Matt

Date: 3 Feb, 2009 18:20:03

Message: 3 of 3

"Heywood " <heywoodj123@yahoo.com> wrote in message <gm9s6m$l3t$1@fred.mathworks.com>...
> "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);
>

Another possibility

 num_dec = 9; % this is your 01001
 num_bin = dec2bin(num_dec,5);

result=-2*(num_bin-'0') +1;

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us