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:
Find

Subject: Find

From: Alex

Date: 3 Nov, 2010 09:26:03

Message: 1 of 12

I have a vector filled with one and zeros.
like this
0 1 1 1 0 0 1 1 0 1 1 0 1 1 1 0 0 0 1 1 1

I want to find how many "1 1" appear ( 2 successively 1 appear) then
I want to find how many "1 1 1" appear (3 successively 1 appear) and so on.

Could you please tell me if there is any function that searches for patterns?

Best Regards
Alex

Subject: Find

From: Jan Simon

Date: 3 Nov, 2010 10:38:04

Message: 2 of 12

Dear Alex,

> 0 1 1 1 0 0 1 1 0 1 1 0 1 1 1 0 0 0 1 1 1
> I want to find how many "1 1" appear ( 2 successively 1 appear)

Depending on the exact definition of your problem, either
  x = [0 1 1 1 0 0 1 1 0 1 1 0 1 1 1 0 0 0 1 1 1];
  numberOf2Ones = length(findstr(x, [1, 1]));
or:
  numberOf2Ones = length(findstr(x, [0, 1, 1, 0]));

See also:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/269584#706441

Kind regards, Jan

Subject: Find

From: Alex

Date: 3 Nov, 2010 11:37:03

Message: 3 of 12

Thanks a lot it worked.

But now I have to understand how the mechanism works:
why [1, 1] is considered as string? For me strings are denoted by ' ' like '[1, 1]'.

The reason for this is that I need to search for different lengths of ones and zeros
like
[1,1]
[1,1,1]
[1,1,1,1]
[1,1,1,1,1]
and so on.
So I need to implement such a mechanism that creates ones and zeros.

Regards
Alex


"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <iare2c$7sd$1@fred.mathworks.com>...
> Dear Alex,
>
> > 0 1 1 1 0 0 1 1 0 1 1 0 1 1 1 0 0 0 1 1 1
> > I want to find how many "1 1" appear ( 2 successively 1 appear)
>
> Depending on the exact definition of your problem, either
> x = [0 1 1 1 0 0 1 1 0 1 1 0 1 1 1 0 0 0 1 1 1];
> numberOf2Ones = length(findstr(x, [1, 1]));
> or:
> numberOf2Ones = length(findstr(x, [0, 1, 1, 0]));
>
> See also:
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/269584#706441
>
> Kind regards, Jan

Subject: Find

From: Alex

Date: 3 Nov, 2010 13:08:04

Message: 4 of 12

I have created a small loop to create strings

strfirstpart='[1';
strlastpart=']';
strmiddlepart=',1';
for j=1:i-1
    strmiddlepart=strcat(strmiddlepart,',1');
end
mystring=strcat(strfirstpart,strmiddlepart,strlastpart);

The above loops creates something like that:
[1,1]
[1,1,1]
[1,1,1,1]
[1,1,1,1,1]
[1,1,1,1,1,1]
[1,1,1,1,1,1,1]

The problem is that

length(findstr(data, (mystring)));

returns nothing. Seems that I pass in the findstr a string '[1,1,1]' but I do not want to compare a string as my data are logical values in the array.

Regards

Subject: Find

From: Alex

Date: 3 Nov, 2010 14:27:04

Message: 5 of 12

I solved the above by creating vectors of numbers instead of strings.

Now one more problem.
what if I want to find only the exact sequence
1,1,1

but not the the two 1,1 sequences that can be found inside 1,1,1

What do you suggest me do?

Subject: Find

From: Oleg Komarov

Date: 3 Nov, 2010 15:30:06

Message: 6 of 12

"Alex " <alaios@yahoo.com> wrote in message <iarrfo$n9g$1@fred.mathworks.com>...
> I solved the above by creating vectors of numbers instead of strings.
>
> Now one more problem.
> what if I want to find only the exact sequence
> 1,1,1
>
> but not the the two 1,1 sequences that can be found inside 1,1,1
>
> What do you suggest me do?

You can use:
http://www.mathworks.com/matlabcentral/fileexchange/28113

Findseq will find all sequences and their lengths.

Oleg

Subject: Find

From: Roger Stafford

Date: 3 Nov, 2010 17:33:04

Message: 7 of 12

"Alex " <alaios@yahoo.com> wrote in message <iar9rb$91e$1@fred.mathworks.com>...
> I have a vector filled with one and zeros.
> like this
> 0 1 1 1 0 0 1 1 0 1 1 0 1 1 1 0 0 0 1 1 1
>
> I want to find how many "1 1" appear ( 2 successively 1 appear) then
> I want to find how many "1 1 1" appear (3 successively 1 appear) and so on.
>
> Could you please tell me if there is any function that searches for patterns?
>
> Best Regards
> Alex
- - - - - - - -
  You can use 'diff' to do what you want. Let x be a row vector of your ones and zeros.

 d = diff([false,x==1,false]);
 f1 = find(d==1);
 f2 = find(d==-1);
 b = histc(f2-f1,1:length(x));

The bin counts in b give the numbers of occurrences of sequences of successive ones of the various possible lengths.

Roger Stafford

Subject: Find

From: Alex

Date: 3 Nov, 2010 17:43:04

Message: 8 of 12

I would like to thank you for your feedback.
So I have used the function like this

 findseq(int8(temp(1,1:30000)))

but I get some output that I can not understand


   1 127 127 7
    1 127 127 2
    1 127 127 2
    1 127 127 3
    0 127 127 8
    0 127 127 2
    0 127 127 2
    1 127 127 4
    1 127 127 4
    0 127 127 7
    1 127 127 3
    0 127 127 3

How comes that all have the same starting and ending position (2nd and 3rd column?)

Regards
 Alex

Subject: Find

From: Oleg Komarov

Date: 3 Nov, 2010 22:56:03

Message: 9 of 12

"Alex " <alaios@yahoo.com> wrote in message <ias6v7$6le$1@fred.mathworks.com>...
> I would like to thank you for your feedback.
> So I have used the function like this
>
> findseq(int8(temp(1,1:30000)))
>
> but I get some output that I can not understand
>
>
> 1 127 127 7
> 1 127 127 2
> 1 127 127 2
> 1 127 127 3
> 0 127 127 8
> 0 127 127 2
> 0 127 127 2
> 1 127 127 4
> 1 127 127 4
> 0 127 127 7
> 1 127 127 3
> 0 127 127 3
>
> How comes that all have the same starting and ending position (2nd and 3rd column?)
>
> Regards
> Alex

Can you email me a sample input, I'll look into that (a possible bug).

Oleg

Subject: Find

From: Alex

Date: 4 Nov, 2010 09:28:04

Message: 10 of 12

I think the problem was the int8.
tried it with int16 and it works fine now.
Could you please think why int8 made it not work. The int8 conversion was done a logical input.

Regards
Alex.

Subject: Find

From: Alex

Date: 4 Nov, 2010 10:54:04

Message: 11 of 12

"Alex " <alaios@yahoo.com> wrote in message <iatub4$391$1@fred.mathworks.com>...
> I think the problem was the int8.
> tried it with int16 and it works fine now.
> Could you please think why int8 made it not work. The int8 conversion was done a logical input.
>
> Regards
> Alex.

Thanks a lot Oleg for your help and your function provided. Could you please tell me if your code can also find (With some small change) when a number appear only once ? right now I think your code "captures" every sequence of same numbers is equal or bigger than 2.

Regards
Alex

Subject: Find

From: Oleg Komarov

Date: 4 Nov, 2010 14:33:03

Message: 12 of 12

"Alex " <alaios@yahoo.com> wrote in message <iau3cc$ovh$1@fred.mathworks.com>...
> "Alex " <alaios@yahoo.com> wrote in message <iatub4$391$1@fred.mathworks.com>...
> > I think the problem was the int8.
> > tried it with int16 and it works fine now.
> > Could you please think why int8 made it not work. The int8 conversion was done a logical input.
> >
> > Regards
> > Alex.
>
> Thanks a lot Oleg for your help and your function provided. Could you please tell me if your code can also find (With some small change) when a number appear only once ? right now I think your code "captures" every sequence of same numbers is equal or bigger than 2.
>
> Regards
> Alex

The problem with an int8 is that it assumes value on the interval [-128, 127];

I will add in the next feature "int" compatibility.

My function treats only sequences of >= 2 numbers...but you can use this submission to get a synthetic result but yet complete with all the occurences:
http://www.mathworks.com/matlabcentral/fileexchange/6436

Oleg

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