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

New to MATLAB?

Thread Subject:
Find rising sequences in index vector

Subject: Find rising sequences in index vector

From: Johannes Korsawe

Date: 30 Aug, 2011 11:00:26

Message: 1 of 3

Dear community,

today i have an indexing task. i could easily solve it via loops, but i would like to have a more elegant solution. The task is given as follows:

i have an index vector I with a sorted array of numbers, e.g.
I=[ 13 29 30 31 32 33 34 35 122 136 137 138 140 255 256 257 313 315 316 317 318 319 320 321 322 323 337 338 339 340];

I find "rising sequences" inside this vector, namely [29, 30, ... ,35] as the first, [136, 137, 138] as the second and so on. Additionally i would like to make the condition, that the sequence should only be output if its length is at least N, e.g. N=3, but N should be a parameter.

I would like to determine these sequences automatically and elegant without loops.

Best regards and thank you,

Subject: Find rising sequences in index vector

From: Bruno Luong

Date: 30 Aug, 2011 11:30:27

Message: 2 of 3

This file in FEX might be useful:


[c l] = SplitVec(I, 'cons', 'split', 'length');

% Desplay

Subject: Find rising sequences in index vector

From: ImageAnalyst

Date: 30 Aug, 2011 11:24:31

Message: 3 of 3

You can threshold the diff function
risingRuns = diff(yourSequence) == 1;
Then to find runs of N elements or longer you can easily to this with
a call to regionprops, if you have the Image Processing Toolbox, or
with a few more complicated statements if you don't. Let us know if
you need additional help. It will require a little bit of tweaking if
you have consecutive runs (like 3 4 5 11 12 13), and to handle the
fact that diff returns a vector one less than the original, but that's
the basic concept.

Tags for 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