How do I find the number of occurrences of data points between specific values in a matrix?
1 view (last 30 days)
Show older comments
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
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')?
Accepted Answer
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)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!