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:
finding consecutive 0's in a vector and replacing it by 1's

Subject: finding consecutive 0's in a vector and replacing it by 1's

From: Bal kishore Pandey

Date: 22 Nov, 2011 14:40:09

Message: 1 of 8

Dear matlab user,
I have a 1x1000 vector, consisting of 1's and 0's.
Now i would like to find four consecutive 0's in the vector and replace it by combination of 0's and 1's (for eg, 1101,1111,1010 any combination of the binary values from 1 to 15) but i should not touch or affect already existing 1's in the vector.

Any help would be really appreciated, as i am new to matlab i find it very difficult.

i found this http://www.mathworks.com/matlabcentral/fileexchange/24255-consecutive-vector-spliter
but i couldn't understand the code.

Thank you very much in advance!!

Subject: finding consecutive 0's in a vector and replacing it by 1's

From: Bruno Luong

Date: 22 Nov, 2011 14:54:09

Message: 2 of 8

"Bal kishore Pandey" <balkishore.pandey@gmail.com> wrote in message <jagc89$9nk$1@newscl01ah.mathworks.com>...
> Dear matlab user,
> I have a 1x1000 vector, consisting of 1's and 0's.
> Now i would like to find four consecutive 0's in the vector and replace it by combination of 0's and 1's (for eg, 1101,1111,1010 any combination of the binary values from 1 to 15) but i should not touch or affect already existing 1's in the vector.
>
> Any help would be really appreciated, as i am new to matlab i find it very difficult.
>
> i found this http://www.mathworks.com/matlabcentral/fileexchange/24255-consecutive-vector-spliter
> but i couldn't understand the code.
>
> Thank you very much in advance!!

Please try this:

A=[1 1 1 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1]

c = SplitVec(A)
c(cellfun(@(x) isequal(x,[0 0 0 0]), c)) = {[1 1 0 1]}
Amodified = cat(2,c{:})

% Bruno

Subject: finding consecutive 0's in a vector and replacing it by 1's

From: Bal kishore Pandey

Date: 22 Nov, 2011 15:12:08

Message: 3 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jagd2h$cfn$1@newscl01ah.mathworks.com>...
> "Bal kishore Pandey" <balkishore.pandey@gmail.com> wrote in message <jagc89$9nk$1@newscl01ah.mathworks.com>...
> > Dear matlab user,
> > I have a 1x1000 vector, consisting of 1's and 0's.
> > Now i would like to find four consecutive 0's in the vector and replace it by combination of 0's and 1's (for eg, 1101,1111,1010 any combination of the binary values from 1 to 15) but i should not touch or affect already existing 1's in the vector.
> >
> > Any help would be really appreciated, as i am new to matlab i find it very difficult.
> >
> > i found this http://www.mathworks.com/matlabcentral/fileexchange/24255-consecutive-vector-spliter
> > but i couldn't understand the code.
> >
> > Thank you very much in advance!!
>
> Please try this:
>
> A=[1 1 1 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1]
>
> c = SplitVec(A)
> c(cellfun(@(x) isequal(x,[0 0 0 0]), c)) = {[1 1 0 1]}
> Amodified = cat(2,c{:})
>
> % Bruno

first of al Thank you very much bruno, it works the same way i wanted it to work.
But in this i only replace the four consecutive zeros with 1101, what if i want to replace it with random of one of these values(1101,0011,0100,0110,0111,1010,1011,1100,1110,1111)
Thank you very much in advance

Subject: finding consecutive 0's in a vector and replacing it by 1's

From: Hin Kwan Wong

Date: 22 Nov, 2011 16:27:08

Message: 4 of 8

> first of al Thank you very much bruno, it works the same way i wanted it to work.
> But in this i only replace the four consecutive zeros with 1101, what if i want to replace it with random of one of these values(1101,0011,0100,0110,0111,1010,1011,1100,1110,1111)
> Thank you very much in advance

Randpool = {[1 1 0 1],[0 0 1 1], [0 1 0 0 ], [0 1 1 0],[0 1 1 1],[1 0 1 0],[1 0 1 1],[1 1 0 0],[1 1 1 0],[1 1 1 1]};
c = SplitVec(A);
logicIndC = cellfun(@(x) isequal(x,[0 0 0 0]), c);
NumberOfReplacement = length(logicIndC(logicIndC));
c(celllogicIndC)= Randpool(randi(length(Randpool),1,NumberOfReplacement));
Amodified = cat(2,c{:})

Subject: finding consecutive 0's in a vector and replacing it by 1's

From: Hin Kwan Wong

Date: 22 Nov, 2011 16:34:10

Message: 5 of 8

Sorry there was a typo with c(celllogicIndC) , should have been c(logicIndC)

==============
Randpool = {[1 1 0 1],[0 0 1 1], [0 1 0 0 ], [0 1 1 0],[0 1 1 1],[1 0 1 0],[1 0 1 1],[1 1 0 0],[1 1 1 0],[1 1 1 1]};
c = SplitVec(A);
logicIndC = cellfun(@(x) isequal(x,[0 0 0 0]), c);
NumberOfReplacement = length(logicIndC(logicIndC));
c(logicIndC) = Randpool(randi(length(Randpool),1,NumberOfReplacement));
Amodified = cat(2,c{:})
================
 

Subject: finding consecutive 0's in a vector and replacing it by 1's

From: ade77

Date: 22 Nov, 2011 18:54:08

Message: 6 of 8

NOTE : The above solutions are correct and the best way to go.

Below is just for informational purpose only, for those who are keen with regular expression.

Rnd = {[1 1 0 1],[0 0 1 1], [0 1 0 0 ], [0 1 1 0],[0 1 1 1],[1 0 1 0],[1 0 1 1],[1 1 0 0],[1 1 1 0],[1 1 1 1]};
v = round(rand(1,1000)); % your vector of length 1000

v = str2double(regexprep(num2str(v), num2str([0 0 0 0]), num2str(Rnd{(randi(10,1))})));

Subject: finding consecutive 0's in a vector and replacing it by 1's

From: Leo Kay

Date: 18 Jan, 2012 15:34:08

Message: 7 of 8

"Hin Kwan Wong" <hin.wong@warwick.ac.uk> wrote in message <jagiu2$4pg$1@newscl01ah.mathworks.com>...
> Sorry there was a typo with c(celllogicIndC) , should have been c(logicIndC)
>
> ==============
> Randpool = {[1 1 0 1],[0 0 1 1], [0 1 0 0 ], [0 1 1 0],[0 1 1 1],[1 0 1 0],[1 0 1 1],[1 1 0 0],[1 1 1 0],[1 1 1 1]};
> c = SplitVec(A);
> logicIndC = cellfun(@(x) isequal(x,[0 0 0 0]), c);
> NumberOfReplacement = length(logicIndC(logicIndC));
> c(logicIndC) = Randpool(randi(length(Randpool),1,NumberOfReplacement));
> Amodified = cat(2,c{:})
> ================
>

Hello Bruno, you have inavertendly helped many other people answering to the author's question.

Subject: finding consecutive 0's in a vector and replacing it by 1's

From: Matt J

Date: 19 Jan, 2012 10:40:10

Message: 8 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jagd2h$cfn$1@newscl01ah.mathworks.com>...
> "Bal kishore Pandey" <balkishore.pandey@gmail.com> wrote in message <jagc89$9nk$1@newscl01ah.mathworks.com>...
>
> Please try this:
>
> A=[1 1 1 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1]
>
> c = SplitVec(A)
> c(cellfun(@(x) isequal(x,[0 0 0 0]), c)) = {[1 1 0 1]}
> Amodified = cat(2,c{:})
============


Another way:

A=[1 1 1 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1];

f=@(x) char([1 x 1]);
Amodified=double(strrep(f(A),f([0 0 0 0]), f([1 1 0 1])));
Amodified=Amodified(2:end-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