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:
vector and index

Subject: vector and index

From: Konst

Date: 2 Mar, 2011 16:08:04

Message: 1 of 8

I have a column vector with ones and zeros e.g. b=[0 0 1 1 1 0 0 1].
I want to start 'reading' the table while doing the following: If c=0.5 , k=0 and d=0 my initial times, every time a b(i)>c then d=d+1..So in the above example I should have d=4. I can do that, It's easy..So here is my problem: I want k=k+1 but only the times that zeros turn to ones while reading the vector (only in the beginning of a "series of ones"), so here I should have f=2.
Here is my code:
d=0;
c=0.5;
k=0;
for i=1:length(b)
    if b(i)>c
        k=k+1; %this is my problem..
        qk
     d=d+1;
    end
end
k
d

Any thoughts will be really appreciated!

Subject: vector and index

From: Konst

Date: 2 Mar, 2011 16:23:07

Message: 2 of 8


Sorry I meant k=2 not f and the right code is

> d=0;
> c=0.5;
> k=0;
> for i=1:length(b)
> if b(i)>c
> k=k+1; %this is my problem..
> d=d+1;
> end
> end
> k
> d

Subject: vector and index

From: Bruno Luong

Date: 2 Mar, 2011 18:22:07

Message: 3 of 8

"Konst " <konstance1@hotmail.com> wrote in message <iklq14$esm$1@fred.mathworks.com>...
> I have a column vector with ones and zeros e.g. b=[0 0 1 1 1 0 0 1].

To me it looks like a *row* vector.

> I want to start 'reading' the table while doing the following: If c=0.5 , k=0 and d=0 my initial times, every time a b(i)>c then d=d+1..So in the above example I should have d=4. I can do that, It's easy..So here is my problem: I want k=k+1 but only the times that zeros turn to ones while reading the vector (only in the beginning of a "series of ones"), so here I should have f=2.

What about the following test, which returns TRUE only when we are at the beginning of sequence of 1s:
   b(i)==1 & (i==1 || b(i-1)==0)

% Bruno

> Here is my code:
> d=0;
> c=0.5;
> k=0;
> for i=1:length(b)
> if b(i)>c
> k=k+1; %this is my problem..
> qk
> d=d+1;
> end
> end
> k
> d
>
> Any thoughts will be really appreciated!

Subject: vector and index

From: Konst

Date: 2 Mar, 2011 20:08:05

Message: 4 of 8

I tried it but it always returns k=0...Can't figure out why..Thanks though Bruno!

Subject: vector and index

From: Bruno Luong

Date: 2 Mar, 2011 20:33:05

Message: 5 of 8

"Konst " <konstance1@hotmail.com> wrote in message <ikm835$2eg$1@fred.mathworks.com>...
> I tried it but it always returns k=0...Can't figure out why..Thanks though Bruno!

But I do get 2

b=[0 0 1 1 1 0 0 1];
d=0;
c=0.5;
k=0;
for i=1:length(b)
    if b(i)==1 && (i==1 || b(i-1)==0)
        k=k+1; %this is NOT my problem..
        d=d+1;
    end
end
k
d

% Bruno

Subject: vector and index

From: someone

Date: 2 Mar, 2011 20:52:04

Message: 6 of 8

"Konst " <konstance1@hotmail.com> wrote in message <iklq14$esm$1@fred.mathworks.com>...
> I have a column vector with ones and zeros e.g. b=[0 0 1 1 1 0 0 1].
> I want to start 'reading' the table while doing the following: If c=0.5 , k=0 and d=0 my initial times, every time a b(i)>c then d=d+1..So in the above example I should have d=4. I can do that, It's easy..So here is my problem: I want k=k+1 but only the times that zeros turn to ones while reading the vector (only in the beginning of a "series of ones"), so here I should have f=2.
> Here is my code:
> d=0;
> c=0.5;
> k=0;
> for i=1:length(b)
> if b(i)>c
> k=k+1; %this is my problem..
> qk
> d=d+1;
> end
> end
> k
> d
>
> Any thoughts will be really appreciated!

% One way (without a loop):

b = [0 0 1 1 1 0 0 1];
k = sum(diff(b) > 0)

k =

     2

Subject: vector and index

From: Konst

Date: 3 Mar, 2011 11:35:22

Message: 7 of 8

"someone" wrote in message <ikmalk$ijm$1@fred.mathworks.com>...
> "Konst " <konstance1@hotmail.com> wrote in message <iklq14$esm$1@fred.mathworks.com>...
> > I have a column vector with ones and zeros e.g. b=[0 0 1 1 1 0 0 1].
> > I want to start 'reading' the table while doing the following: If c=0.5 , k=0 and d=0 my initial times, every time a b(i)>c then d=d+1..So in the above example I should have d=4. I can do that, It's easy..So here is my problem: I want k=k+1 but only the times that zeros turn to ones while reading the vector (only in the beginning of a "series of ones"), so here I should have f=2.
> > Here is my code:
> > d=0;
> > c=0.5;
> > k=0;
> > for i=1:length(b)
> > if b(i)>c
> > k=k+1; %this is my problem..
> > qk
> > d=d+1;
> > end
> > end
> > k
> > d
> >
> > Any thoughts will be really appreciated!
>
> % One way (without a loop):
>
> b = [0 0 1 1 1 0 0 1];
> k = sum(diff(b) > 0)
>
> k =
>
> 2

Perfect!!Thank you very much...

Subject: vector and index

From: Jos (10584)

Date: 3 Mar, 2011 15:53:20

Message: 8 of 8

"Konst " <konstance1@hotmail.com> wrote in message
* SNIP *
> > > Any thoughts will be really appreciated!
> >
> > % One way (without a loop):
> >
> > b = [0 0 1 1 1 0 0 1];
> > k = sum(diff(b) > 0)
> >
> > k =
> >
> > 2
>
> Perfect!!Thank you very much...

Another option:

b = [0 0 1 1 1 0 0 1] ;
ix = strfind(b,[0 1])
k = numel(ix)

~ Jos

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