|
On Jun 15, 7:18 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> Lucy <comtech....@gmail.com> wrote in message <75f39778-f788-46b7-8927-1f381338e...@f16g2000vbl.googlegroups.com>...
> > Hi all,
>
> > I hvae a vector of 1, 0, -1s. I want to find the histogram of the
> > number of consecutive numbers, ie the histogram of the runs.
>
> > For example, how many two 1s, three 1s, four 1s in a row, and those of
> > -1s, etc.
>
> > How to do this fast? Hopefully without for loops?
>
> > Thx a lot
>
> Let v be a row vector.
>
> p = diff(find([true,diff(v)~=0,true]));
> b = histc(p,1:max(p));
>
> Roger Stafford
Thanks Roger but, ...
>> v=[1 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1];
p = diff(find([true,diff(v)~=0,true]));
b = histc(p,1:max(p));
>> b
b =
2 2 2 1
That's not right...
>> v=[1 1 1 0 0 -1 -1 0 -1 1 1 1 1 0 0 -1 0 1 0 1 1];
p = diff(find([true,diff(v)~=0,true]));
b = histc(p,1:max(p));
>>
>>
>> p
p =
3 2 2 1 1 4 2 1 1 1 1
2
>> b
b =
6 4 1 1
--------------------
I have to separate the runs of 1s and 0s and -1s...
ie. for 1s, I generate a histogram,
for 0s, I generate another historgram
for -1s, one more...
How to do that?
Thank you!
|