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:
how to find the kth-element in each consecutive run of numbers?

Subject: how to find the kth-element in each consecutive run of numbers?

From: Luna Moon

Date: 4 Feb, 2010 03:23:42

Message: 1 of 6

Let's say I have a long vector of 0-1 numbers:

0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0

Here 1's are our targets.

How to find the location of the first kth element of 1 in the 1-
blocks?

And

How to find the location of the last kth element of 1 in the 1-
blocks?

And

How to find the location of the median position of 1 in the 1-blocks?
(i.e. if there are 5 1's, then the median position should be the 3rd
1's location)

How to do this efficiently?

(not asking for something involving lots of for loops...)

Thanks

Subject: how to find the kth-element in each consecutive run of numbers?

From: TideMan

Date: 4 Feb, 2010 03:43:00

Message: 2 of 6

On Feb 4, 4:23 pm, Luna Moon <lunamoonm...@gmail.com> wrote:
> Let's say I have a long vector of 0-1 numbers:
>
> 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
> 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
>
> Here 1's are our targets.
>
> How to find the location of  the first kth element of 1 in the 1-
> blocks?
>
> And
>
> How to find the location of  the last kth element of 1 in the 1-
> blocks?
>
> And
>
> How to find the location of the median position of 1 in the 1-blocks?
> (i.e. if there are 5 1's, then the median position should be the 3rd
> 1's location)
>
> How to do this efficiently?
>
> (not asking for something involving lots of for loops...)
>
> Thanks

t=[ 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]';
dt=diff(t);
istart=find(dt>0) + 1;
ifin=find(dt<0);

Now, you have the start and finish addresses of each block of 1s.
I'll leave you to figure out how to get the median of each block.
It's no problem for an odd number of 1s, but what will you do for an
even number?

Subject: how to find the kth-element in each consecutive run of numbers?

From: Jos (10584)

Date: 4 Feb, 2010 07:22:02

Message: 3 of 6

Luna Moon <lunamoonmoon@gmail.com> wrote in message <8a256245-dc72-4fb2-a05e-02b9829bbb39@f5g2000vbm.googlegroups.com>...
> Let's say I have a long vector of 0-1 numbers:
>
> 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
> 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
>
> Here 1's are our targets.
>
> How to find the location of the first kth element of 1 in the 1-
> blocks?
>
> And
>
> How to find the location of the last kth element of 1 in the 1-
> blocks?
>
> And
>
> How to find the location of the median position of 1 in the 1-blocks?
> (i.e. if there are 5 1's, then the median position should be the 3rd
> 1's location)
>
> How to do this efficiently?
>
> (not asking for something involving lots of for loops...)
>
> Thanks

V = [0 0 1 1 0 0 0 1 1 1 1 0 0]
k = 2 ;
idx = strfind(V,[0 1])+k

hth
Jos

Subject: how to find the kth-element in each consecutive run of numbers?

From: Luna Moon

Date: 4 Feb, 2010 13:20:27

Message: 4 of 6

On Feb 3, 10:43 pm, TideMan <mul...@gmail.com> wrote:
> On Feb 4, 4:23 pm, Luna Moon <lunamoonm...@gmail.com> wrote:
>
>
>
> > Let's say I have a long vector of 0-1 numbers:
>
> > 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
> > 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
>
> > Here 1's are our targets.
>
> > How to find the location of  the first kth element of 1 in the 1-
> > blocks?
>
> > And
>
> > How to find the location of  the last kth element of 1 in the 1-
> > blocks?
>
> > And
>
> > How to find the location of the median position of 1 in the 1-blocks?
> > (i.e. if there are 5 1's, then the median position should be the 3rd
> > 1's location)
>
> > How to do this efficiently?
>
> > (not asking for something involving lots of for loops...)
>
> > Thanks
>
> t=[ 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
> 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]';
> dt=diff(t);
> istart=find(dt>0) + 1;
> ifin=find(dt<0);
>
> Now, you have the start and finish addresses of each block of 1s.
> I'll leave you to figure out how to get the median of each block.
> It's no problem for an odd number of 1s, but what will you do for an
> even number?

take either of the middle two...

any more thoughts?

Subject: how to find the kth-element in each consecutive run of numbers?

From: Luna Moon

Date: 4 Feb, 2010 13:22:47

Message: 5 of 6

On Feb 4, 2:22 am, "Jos (10584) " <#10...@fileexchange.com> wrote:
> Luna Moon <lunamoonm...@gmail.com> wrote in message <8a256245-dc72-4fb2-a05e-02b9829bb...@f5g2000vbm.googlegroups.com>...
> > Let's say I have a long vector of 0-1 numbers:
>
> > 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
> > 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
>
> > Here 1's are our targets.
>
> > How to find the location of  the first kth element of 1 in the 1-
> > blocks?
>
> > And
>
> > How to find the location of  the last kth element of 1 in the 1-
> > blocks?
>
> > And
>
> > How to find the location of the median position of 1 in the 1-blocks?
> > (i.e. if there are 5 1's, then the median position should be the 3rd
> > 1's location)
>
> > How to do this efficiently?
>
> > (not asking for something involving lots of for loops...)
>
> > Thanks
>
> V = [0 0 1 1 0 0 0 1 1 1 1 0 0]
> k = 2 ;
> idx = strfind(V,[0 1])+k
>
> hth
> Jos

Great! But for median?

How to?

Subject: how to find the kth-element in each consecutive run of numbers?

From: Jos (10584)

Date: 4 Feb, 2010 13:33:19

Message: 6 of 6

Luna Moon <lunamoonmoon@gmail.com> wrote in message <2ae7b70f-d0b4-4042-957b-51e4f07f25f2@f5g2000vbm.googlegroups.com>...
> On Feb 4, 2:22 am, "Jos (10584) " <#10...@fileexchange.com> wrote:
> > Luna Moon <lunamoonm...@gmail.com> wrote in message <8a256245-dc72-4fb2-a05e-02b9829bb...@f5g2000vbm.googlegroups.com>...
> > > Let's say I have a long vector of 0-1 numbers:
> >
> > > 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
> > > 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
> >
> > > Here 1's are our targets.
> >
> > > How to find the location of  the first kth element of 1 in the 1-
> > > blocks?
> >
> > > And
> >
> > > How to find the location of  the last kth element of 1 in the 1-
> > > blocks?
> >
> > > And
> >
> > > How to find the location of the median position of 1 in the 1-blocks?
> > > (i.e. if there are 5 1's, then the median position should be the 3rd
> > > 1's location)
> >
> > > How to do this efficiently?
> >
> > > (not asking for something involving lots of for loops...)
> >
> > > Thanks
> >
> > V = [0 0 1 1 0 0 0 1 1 1 1 0 0]
> > k = 2 ;
> > idx = strfind(V,[0 1])+k
> >
> > hth
> > Jos
>
> Great! But for median?
>
> How to?

strfind([0 V],[0 1]) gives you the start positions, and strfind([V 0],[1 0]) gives you the final position of the runs. I leave it you:

1) if you should add or subtract 1 from these positions
2) why padding V with a zero is useful
3) how to get the intermediate positions from these values

hth
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