Sunday, 8 September 2013

Count numbers of consecutive 1s and 0s in a vector

Count numbers of consecutive 1s and 0s in a vector

In Matlab I have a vectors that looks like this:
0 0 1 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 0 0 1 0 1
What I want to do now is to count the number of 1 in this vector.
Consecutive 1s count as 1. Additionally, I want also to calculate the
average and median numbers of 0s between 1s. So for this example:
1s: 5
Median 0s: 3.5
Average 0s: 3
I solved that with a brute force method, that is investigate each element
in a loop and check the previous as well as the next element. But I'm sure
there has to be a solution that is way faster. Any idea?

No comments:

Post a Comment