How do I find the number of occurrences of data points between specific values in a matrix?

1 view (last 30 days)
The following code finds the number of occurrences of data between two points of the same value.
x=[4 1 2 2 2 2 2 2 2 2 2 4 1 2 2 2 2 3 3 4 5 6 1 2 2 2 2 2 2 2 2 2 2 2 3 4 1 2 2 2 2 2];
t=data==8;
out=diff(find(t))-1;
How could one change this code to determine how many data points are between specified data points to choose between?
for example:
If I had this code and I wanted to find the interval between the "1" and the next "4" in sequence how would I go about doing it? I want the code to output the interval of data points between the two specific values for each of the occurrences. From the data given as X the output should be output=[9 6 12]
Any help would be appreciated
  2 Comments
dpb
dpb on 9 Dec 2015
How much is known of the data structure a priori? Specifically, is it known the values exist in sequence as shown here (albeit a missing closing '4' after the last initial '1')?
Joe
Joe on 14 Dec 2015
this is just an example data set but the one I normally work with is a 1x600 matrix consisting of values between 1 and 10. It is known that these values occur in a semi-structured way. for example a 1 always precedes a 2 but a 1 can occur without being followed by a 2. I wanted a way to specifically target two numbers in a sequence and find their difference. The problem I am finding is that when I type in the code:
data=[1 2 2 2 2 2 2 2 2 2 3 1 2 2 2 2 7 8 3 8 3 4 5 6 1 2 2 2 ...
2 2 2 2 2 2 2 2 3 4 1 2 2 2 2 2];
PEI1=data== 1;
PEI3=data==3
PEI1out=diff(find(PEI1-PEI3))-1
it gives me a half right answer PEIout= [9 0 6 1 3 11 1], where the 9, 6, and 11 are correct but the others are not.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 14 Dec 2015
data = [1 2 2 2 2 2 2 2 2 2 3 1 2 2 2 2 7 8 3 8 3 4 5 6 1 2 2 2 ...
2 2 2 2 2 2 2 2 3 4 1 2 2 2 2 2];
x = [1 3];
[l1,ii] = ismember(data,x);
i0 = find(l1);
k = strfind(ii(i0),[1 2]);
out = diff([i0(k);i0(k+1)])-1;

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!