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:
Counting the oberserved runs in a sequence

Subject: Counting the oberserved runs in a sequence

From: oli8819

Date: 3 Jul, 2013 09:58:09

Message: 1 of 6

I have got a sequence which is made of 0's and 1's.
What I am looking to do is count the number of runs the sequence

For example A=[1 1 1 1 1 0 0 0 1 1 1 0 0 0 0 1]

1 run is: 1 1 1 1 1
2nd run is: 0 0 0
3rd run: 1 1 1
4th run: 0 0 0 0
5th run is: 1

Therefore there is 5 runs in this sequence
Any help would be much appreciated!

Subject: Counting the oberserved runs in a sequence

From: Lothar Schmidt

Date: 3 Jul, 2013 10:27:54

Message: 2 of 6

Am 03.07.2013 11:58, schrieb oli8819:
> A=[1 1 1 1 1 0 0 0 1 1 1 0 0 0 0 1]

i suppose this delivers what you want:

sum(abs(diff(A)))+1

Subject: Counting the oberserved runs in a sequence

From: dpb

Date: 3 Jul, 2013 14:02:14

Message: 3 of 6

On 7/3/2013 5:27 AM, Lothar Schmidt wrote:
> Am 03.07.2013 11:58, schrieb oli8819:
>> A=[1 1 1 1 1 0 0 0 1 1 1 0 0 0 0 1]
>
> i suppose this delivers what you want:
>
> sum(abs(diff(A)))+1

alternatively,


sum(diff(A)~=)+1

--

Subject: Counting the oberserved runs in a sequence

From: dpb

Date: 3 Jul, 2013 14:13:01

Message: 4 of 6

On 7/3/2013 9:02 AM, dpb wrote:
...

> alternatively,
>
>
> sum(diff(A)~=)+1

sum(diff(A)~=0)+1

erratum, missed the '0', sorry... :(

--

Subject: Counting the oberserved runs in a sequence

From: Lothar Schmidt

Date: 4 Jul, 2013 08:05:48

Message: 5 of 6

Am 03.07.2013 16:13, schrieb dpb:
> On 7/3/2013 9:02 AM, dpb wrote:
> ...
>
>> alternatively,
>>
>>
>> sum(diff(A)~=)+1
>
> sum(diff(A)~=0)+1
>
> erratum, missed the '0', sorry... :(
>
> --

not shure if this works by hazard...

sum(~diff(A))-numel(A)

Subject: Counting the oberserved runs in a sequence

From: Lothar Schmidt

Date: 4 Jul, 2013 08:14:28

Message: 6 of 6

Am 04.07.2013 10:05, schrieb Lothar Schmidt:
>
> sum(~diff(A))-numel(A)

vice versa - sorry

numel(A)-sum(~diff(A))

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