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 end-points for runs

Subject: Finding end-points for runs

From: Gautam Sethi

Date: 27 Jan, 2013 01:26:02

Message: 1 of 5

Dear all:
I'm trying to find the end-points for runs of numbers based on a vector. Let

>> Y = [1 3 4 6 7 8]

Y =

     1 3 4 6 7 8

I want an output vector Z of size(length(Y),2) where

Z =

     0 2
     2 5
     2 5
     5 9
     5 9
     5 9

Here is the idea behind what I want.

The first element of Y is 1, which is not followed by the next integer; thus, the integers that "surround" the integer 1 are 0 and 2, the first row of Z.

The second element of Y is 3, which is followed by the next integer, 4. However there is a break after 4. Thus, the run of consecutive integers 3 and 4 is "surrounded" by the integers 2 and 5. Therefore, the next two rows of Z, corresponding to the two elements 3 and 4 of Y, are [2 5].

Likewise, the last three elements of Y are consecutive integers, "surrounded" by the integers 5 and 9. Hence the last three rows of Z are [5 9].

Thanks for your help!

Subject: Finding end-points for runs

From: Roger Stafford

Date: 27 Jan, 2013 06:23:08

Message: 2 of 5

Gautam Sethi <gautamsethi@gmail.com> wrote in message <cd7ccb2d-8c83-4568-a565-22f22a9416bf@googlegroups.com>...
> Dear all:
> I'm trying to find the end-points for runs of numbers based on a vector. Let
>
> >> Y = [1 3 4 6 7 8]
>
> Y =
>
> 1 3 4 6 7 8
>
> I want an output vector Z of size(length(Y),2) where
>
> Z =
>
> 0 2
> 2 5
> 2 5
> 5 9
> 5 9
> 5 9
>
> Here is the idea behind what I want.
>
> The first element of Y is 1, which is not followed by the next integer; thus, the integers that "surround" the integer 1 are 0 and 2, the first row of Z.
>
> The second element of Y is 3, which is followed by the next integer, 4. However there is a break after 4. Thus, the run of consecutive integers 3 and 4 is "surrounded" by the integers 2 and 5. Therefore, the next two rows of Z, corresponding to the two elements 3 and 4 of Y, are [2 5].
>
> Likewise, the last three elements of Y are consecutive integers, "surrounded" by the integers 5 and 9. Hence the last three rows of Z are [5 9].
>
> Thanks for your help!
- - - - - - - - -
 Y = Y';
 f = find([true;diff(Y)>1;true]);
 Z = zeros(size(Y,1),2);
 Z(1,:) = [Y(1)-1,Y(f(2)-1)+1];
 Z(f(2:end-1),:) = diff([Y(f(1:end-1)),Y(f(2:end)-1)],1,1);
 Z = cumsum(Z,1);

Roger Stafford

Subject: Finding end-points for runs

From: Bruno Luong

Date: 27 Jan, 2013 08:17:08

Message: 3 of 5

This is my takes:

b = [true diff(Y)>1]
first = Y(b);
last = Y([b(2:end) true])
Z = [first(:)-1 last(:)+1];
Z = Z(cumsum(b),:)

% Bruno

Subject: Finding end-points for runs

From: Bruno Luong

Date: 27 Jan, 2013 08:27:06

Message: 4 of 5

Just to make it shorter

b = [true diff(Y)>1]
Z = [Y(b)-1; Y([b(2:end) true])+1];
Z = Z(:,cumsum(b))'

% Bruno

Subject: Finding end-points for runs

From: Gautam Sethi

Date: 27 Jan, 2013 12:55:59

Message: 5 of 5

On Sunday, January 27, 2013 3:27:06 AM UTC-5, Bruno Luong wrote:
> Just to make it shorter
>
>
>
> b = [true diff(Y)>1]
>
> Z = [Y(b)-1; Y([b(2:end) true])+1];
>
> Z = Z(:,cumsum(b))'
>
>
>
> % Bruno

Thank you Roger an Bruno. That was immensely helpful!

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