Tuesday, June 09, 2009

Matlab: find 5 consecutive numbers in an array

OP here.

Q:

I want to find blocks of values of ascending order within a vector. e.g.:

[... 10 1 2 3 4 5 56 ...]

and [1 2 3 4 5] should be recognized.

it should also recognise e.g.:

[... 100 50 51 52 53 54 20 ...]


A:

% if not care about precisely 5 number block;
strfind(diff(x), [1 1 1 1])

% if has to be 5 consecutive number;
strfind(diff(x) == 1,[0 1 1 1 1 0])+1

1 comment:

Unknown said...

Hey Siyi,

I had the same issue and solved it this way:

locations = strfind(x', template);

Cheers