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

1 次查看(过去 30 天)
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 个评论
dpb
dpb 2015-12-9
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 2015-12-14
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.

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2015-12-14
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;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by