[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: counting runs of a certain length in binary data
From: |
Mike Miller |
Subject: |
Re: counting runs of a certain length in binary data |
Date: |
Fri, 3 Mar 2000 19:06:06 -0600 (CST) |
On Fri, 3 Mar 2000, John W. Eaton wrote:
> Would the following work?
>
> y = [0; X; 0];
> find (diff (y) == -1) - find (diff (y) == 1)
>
> X is the vector of binary digits. The idea is that prepending and
> appending zeros ensures that the first nonzero element of diff(y) will
> be 1 and that the last nonzero element will be -1, and that there will
> be an equal number of 1 and -1 elements. Then the results of the two
> find commands should always have the same length, and their difference
> should be the run-lengths for the ones in the original vector.
John--
That was great! Thanks much. It wasn't quite what I was looking for, but
it did give me the clue I needed to get what I wanted.
Your code gives run lengths for ones, but I needed run lengths for ones
and for zeros. I used the 'find' idea you presented and altered it as
follows:
diff (find (diff ([2; X; 2]) ~= 0))
The use of '2' is an arbitrary choice: I had to put something other than
zeros or ones in there. That line of code gives me a vector of run
lengths as follows:
octave:1> X=[0 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0]';
octave:2> diff (find (diff ([2; X; 2]) ~= 0))
ans =
3
2
5
6
1
1
7
If I need to count how many runs are length N or longer, as I'd originally
planned, I can use this line of code:
sum (diff (find (diff ([2; X; 2]) ~= 0)) >= N)
Thanks again. That was a big help. It's amazing how much can be done
with Octave in a single compact line.
Mike
-----------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.che.wisc.edu/octave/octave.html
How to fund new projects: http://www.che.wisc.edu/octave/funding.html
Subscription information: http://www.che.wisc.edu/octave/archive.html
-----------------------------------------------------------------------