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:
Help with code to shift and catch 0's and 1's

Subject: Help with code to shift and catch 0's and 1's

From: Jason

Date: 27 Apr, 2011 08:28:04

Message: 1 of 3

Hello, I need help with writing a piece of code.
I have a data file that looks like this, say, one column with many rows as such (but to save space, I'll write it in a single row format):
00100010000
I need help with a code that can re-arrange to look like this:
00011110000

That is, reading from left-to-right, as soon as the first "1" appears, make that value "0" and change the next item from "0" to a "1" and continue until "1" appears again.

Thanks!

Subject: Help with code to shift and catch 0's and 1's

From: dpb

Date: 27 Apr, 2011 12:56:21

Message: 2 of 3

On 4/27/2011 3:28 AM, Jason wrote:
> Hello, I need help with writing a piece of code.
> I have a data file that looks like this, say, one column with many rows
> as such (but to save space, I'll write it in a single row format):
> 00100010000
> I need help with a code that can re-arrange to look like this:
> 00011110000

...

 >> s=[0 0 1 0 0 0 1 0 0 0 0];
 >> idx=find(s==1)
idx =
      3 7
 >> s(idx(1):idx(2)-1)=~s(idx(1):idx(2)-1)
s =
      0 0 0 1 1 1 1 0 0 0 0
 >>

Salt to suit for repeated where may need to determine start/stop
direction of bit flip...

--

Subject: Help with code to shift and catch 0's and 1's

From: Roger Stafford

Date: 27 Apr, 2011 13:49:05

Message: 3 of 3

"Jason " <core_ab56@hotmail.com> wrote in message <ip8k2k$lc$1@fred.mathworks.com>...
> .......
> 00100010000
> I need help with a code that can re-arrange to look like this:
> 00011110000
> .......
- - - - - - - - -
  If x is your original column vector of 1's and 0's (and if I have generalized your example correctly,) do this:

 y = mod(cumsum([0;x(1:end-1)]),2);

Roger Stafford

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