MATLAB: How to find the number of occurrences of data points between specific values in a matrix

interval

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

Best Answer

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;