Basic question: how to find range of index values
Show older comments
I want to find the location (range of N index values) over which a variable (mydata) holds a specific value (X)
The variable (mydata) contains many occurrences of X before the occurrence of a plateau - where the value of X is constant for N data points.
ELABORATION:
My problem is a little more complex.
mydata = [1 1 2 3 4 5 6 1 4 4 4 4 4 4 4 1 45 67 8 9 4 4 4 4 4 1 36 2 4 4 4 3 1 1 18 98 99]
I want to find the index values for the 7 (N) consecutive occurrences of '4'. I do not want to find the other occurrences of '4' i.e. one occurrence before the sequence of 7 and later one occurrence of a sequence of 5.
Accepted Answer
More Answers (6)
Matt Fig
on 13 Jun 2011
mydata = [1 1 2 3 4 5 6 1 4 4 4 4 4 4 4 1 45 67 8 9 4 4 4 4 4 1 36 2 4 4 4 3 1 1 18 98 99];
findstr(mydata,4*ones(1,7))
This finds the starting index of a sequence of 7 4s in a row. Change the 4 to a 5 to find a sequence of 7 5s in a row. Change the 7 to a 3 to find a sequence of 3 4s in a row, etc.
Note that the above will return the starting indices of all occurrences of, say, 3 4s in a row - even where those occurrences are sub-occurrences. To limit the search to only those occurrences which are exactly the length desired, simply make one more call to the function:
S = findstr(mydata,4*ones(1,3));
S = S(findstr([0 diff(S)==1 0],[0 0]))
1 Comment
Andrei Bobrov
on 13 Jun 2011
+1
Laura Proctor
on 13 Jun 2011
If you wish to find the corresponding index values where your variable mydata is equal to X, then you can use the find command like this:
iVals = find(mydata==X);
Chirag Gupta
on 13 Jun 2011
I am not exactly clear about the question, but may be this is a starting point:
mydata = [1 1 2 3 4 5 6 1 1 45 67 8 9 1 36 2 3 1 1 18 98 99];
c = find(mydata==1)
You would probably to refine the condition inside the find, especially if your are dealing with doubles
Laura Proctor
on 13 Jun 2011
The following will find the maximum run and return the index values in that run to a variable named reqVals.
I'm sure there's a tidier way to do this, but off the top of my head:
mydata = [1 1 2 3 4 5 6 1 4 4 4 4 4 4 4 1 45 67 8 9 4 4 4 4 4 1 36 2 4 4 4 3 1 1 18 98 99];
iVals = find(mydata==4);
sVals = iVals([true diff(iVals)~=1]);
eVals = iVals([diff(iVals)~=1 true]);
lVals = eVals - sVals + 1;
[num idx] = max(lVals)
reqVals = sVals(idx):eVals(idx)
Andrei Bobrov
on 13 Jun 2011
md = mydata==4;
vf = find([true diff(md)~=0]);
idx = [vf; vf(2:end)-1 length(md)];
indexsevenfour = idx(:,diff(idx)+1== 7)';
MORE variant
idx = bsxfun(@plus,1:7,(1:length(mydata)-7)'-1);
idx(all(bsxfun(@eq,mydata(idx),4*ones(1,7)),2))
Etienne O'Brien
on 13 Jun 2011
0 votes
2 Comments
Matt Fig
on 13 Jun 2011
See my solution above.
Laura Proctor
on 13 Jun 2011
I would go with Matt's response if you want to find sequence of (7) 4's in a row as that is the simplest way to find the index value of the starting point.
The other methods use logical indexing, which assign values of 1 for true and 0 for false.
Categories
Find more on Creating and Concatenating 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!