Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Finding end-points for runs
Replies: 4   Last Post: Jan 27, 2013 7:55 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Roger Stafford

Posts: 5,873
Registered: 12/7/04
Re: Finding end-points for runs
Posted: Jan 27, 2013 1:23 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.