How to tell the difference between a streak of consecutive 0's vs a streak of consecutive 1's??

1 view (last 30 days)
Let's say I have a sample set of data
Data = [100,200;
300,100;
400, 100
300, 100];
So I want to figure out the longest streak where column one is greater than its adjacent value in column 2.
D = Data(:,1)>Data(:,2)
I am capable of finding the longest streak of either ones or zeros in D by doing:
streak = max(diff(find([1,diff(D'),1])));
in this case
streak = 3
However I want to find out whether the "streak" is a streak of trues or falses.
Because if I changed my data so that it is a streak of 3 zeros instead of 3 ones, my value for
"streak" remains at 3.
Is there a way I can manipulate and add to my code to return a true value for another variable
(say, "flag") if the "streak" is a streak of trues and a false if the "streak" is a streak of
falses???
I am very new to using Matlab and any suggestions would help a lot!

Answers (1)

Star Strider
Star Strider on 31 Oct 2014
Here is one possibility:
Data = [100,200;
300,100;
400, 100
300, 100];
Data = [Data; 100,200; 100,200; Data; 100,200; 100,200; 100,200;];
TF = diff(Data,[],2)<0;
state = [diff([0; TF])>0 diff([0; TF])<0 diff([0; TF])==0];
The ‘TF’ vector is ‘true’ =1 when the second column element in a particular row is less the first. The ‘state’ matrix is such that when ‘TF’ goes from 0 to 1 the corresponding element in the first column is 1, when ‘TF’ goes from 1 to 0 the second column element is 1, and when there is no change, the element in the third column is 1.
Combine these to define your ‘streak’ variable, since I am not certain exactly how you want to do that. Note that the series of 1 values in the third column is actually one element shorter than the actual streak of sequential values in ‘TF’.
The leading zero values in the diff arguments in my ‘state’ matrix are to make the indices correspond to those in ‘TF’.
If you want to see how it all works, combine ‘TF’ and ‘state’ into one matrix (I call it ‘L’ here):
L = [TF state];

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!