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 if a sequence occurs?

Subject: How to find if a sequence occurs?

From: Diego Zegarra

Date: 12 Feb, 2009 20:29:02

Message: 1 of 8

Suppose we have the vector a=[2 4 5 3 7 6] and I want to see if the 4 is after the 2 and so on for every number, 6 after 7, etc.

Anyone can guide me on a way to do this? Thanks so much in advance!

Subject: How to find if a sequence occurs?

From: Eric Williams

Date: 12 Feb, 2009 20:46:01

Message: 2 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gn20qd$2ic$1@fred.mathworks.com>...
> Suppose we have the vector a=[2 4 5 3 7 6] and I want to see if the 4 is after the 2 and so on for every number, 6 after 7, etc.
>
> Anyone can guide me on a way to do this? Thanks so much in advance!

If you mean that you want to check if the vector is strictly increasing, you can use:

strictly_increasing = all(diff(a) > 0);

If all of the numbers in the vector are increasing, (no duplicates, no decreasing), strictly_increasing will be logical 1. Otherwise, it will be logical 0.

Subject: How to find if a sequence occurs?

From: Diego Zegarra

Date: 12 Feb, 2009 21:06:02

Message: 3 of 8

That is not what I meant, I do not care if its increasing or not, I just want to be able to see when a sequence happens.

Subject: How to find if a sequence occurs?

From: Eric Williams

Date: 12 Feb, 2009 21:20:17

Message: 4 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gn22vq$sn9$1@fred.mathworks.com>...
> That is not what I meant, I do not care if its increasing or not, I just want to be able to see when a sequence happens.

What kind of sequence? You simply said "4 is after 2", "6 is after 7". What is a sequence?

Subject: How to find if a sequence occurs?

From: someone

Date: 12 Feb, 2009 21:56:02

Message: 5 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gn20qd$2ic$1@fred.mathworks.com>...
> Suppose we have the vector a=[2 4 5 3 7 6] and I want to see if the 4 is after the 2 and so on for every number, 6 after 7, etc.
>
> Anyone can guide me on a way to do this? Thanks so much in advance!

% Although this is not quite clear,
% you might try findstr or strfind

>> a=[2 4 5 3 7 6];
>> findstr(a,[2 4])

ans =

     1

>> findstr(a,[7 6])

ans =

     5

Subject: How to find if a sequence occurs?

From: Roger Stafford

Date: 12 Feb, 2009 22:30:18

Message: 6 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gn20qd$2ic$1@fred.mathworks.com>...
> Suppose we have the vector a=[2 4 5 3 7 6] and I want to see if the 4 is after the 2 and so on for every number, 6 after 7, etc.
>
> Anyone can guide me on a way to do this? Thanks so much in advance!

  What do your mean by "a sequence occurs" or "a sequence happens"? It is generally not a good idea to make others on CSSM have to guess at your meaning. I'll make just one guess: you want to see if there is a consecutive set of integers present. If that is it,

  all(diff(sort(a))==1) & round(a(1))==a(1)

will be true if so.

Roger Stafford

Subject: How to find if a sequence occurs?

From: Rune Allnor

Date: 12 Feb, 2009 23:35:14

Message: 7 of 8

On 12 Feb, 21:29, "Diego Zegarra" <diego...@gmail.com> wrote:
> Suppose we have the vector a=[2 4 5 3 7 6] and I want to see if the 4 is after the 2 and so on for every number, 6 after 7, etc.
>
> Anyone can guide me on a way to do this? Thanks so much in advance!

Use correlation or a matched filter (which
are different names for the same thing).

Rune

Subject: How to find if a sequence occurs?

From: Jos

Date: 13 Feb, 2009 07:44:04

Message: 8 of 8

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gn20qd$2ic$1@fred.mathworks.com>...
< SNIP ... wants to find if two integers occur one after another

Based on your somewhat confusing description of the problem, I could suggest:

% some data
   A = [2 4 3 6 5 4 2 4 3 1 3 1 3 3 4 4 2 2] ;

% a engine
  B = false(max(A)) ;
  idx = sub2ind(size(B),A(1:end-1),A(2:end)) ;
  B(idx) = true ;

% Do two numbers follow each other?
  n1 = 2 ;
  n2 = 4 ;
  if B(n1,n2), disp('Yes') ; end

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