"find" data range within vector and ensure second index greater than first

I'm analyzing vehicle test data and trying to automatically identify the start and end indices to be analyzed. I'm using the "find" function and trying to utilize flexible thresholds to allow the analysis to work wth the broad range of data I expect to see. Unfortunately this also leaves the script prone to finding points where my end criteria occur before my start criteria.
%% Identify Start and End Data Points
% Identify start and end points to clip data to only that which needs to be
% analyzed.
for ff = 1:length(dataAll)
dataAll(ff).dataStart = find((dataAll(ff).Time > 0 & dataAll(ff).AccActPos > 20 & ...
dataAll(ff).GPSSpeed > 20 & abs(dataAll(ff).SteeringWheelAngle) > 60),1);
dataAll(ff).dataEnd = find((dataAll(ff).Time > 0 & dataAll(ff).GPSSpeed > 25 &...
dataAll(ff).AccActPos < 10 & abs(dataAll(ff).SteeringWheelAngle) < 50,1);
end
clearvars ff
This is the relevant portion of my existing script. It runs, but as noted it can find dataEnd with a lower index than dataStart and this doesn't work for the analysis. dataAll is the struct into which I'm loading data files, and there are always more than one. This is the need for the loop. I'm having a struggle day and can't find what I expect is an easy solution to add a criteria requiring dataEnd to be greater than dataStart. I know I can't simply add dataEnd > dataStart in the find definition for dataEnd since the field itself is yet undefined. I also know I can't create dataEnd and preload it with a value because it'll just evaluate whether or not that preloaded value is greater than dataStart.
If it helps to see something less complex and with data...
A = [0 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 2 1 0]
x = find(A>2,1);
y = find(A<3 & y>x,1);
I know this doesn't work because y>x can't be evaluated since y doesn't yet exist, but this is the net result I'm looking for. How do I force y > x? Is it effective or efficient to simply find all values of y = find(A<3) and then create z = find(y>x,1)?

 Accepted Answer

How about
y = find(A(max(x):end)<3);

3 Comments

Thank you for the reply and suggestion. As it is this isn't providing the result I am seeking, but I think it'll lead me in the right direction. As coded the result is a vector with values of 10, 11, and 12. I only need the first instance, so adding ",1" to the find will fix that. But this is still providing me with the numerical index after x, but does not account for the indices prior to x.
In my example A has 23 elements. x is the 12th element. y should be the 21st element. The above result is telling me that y occurs 10 elements after x, so I can get there with y = y + (x - 1) with the "-1" accounting for starting at x and not the next element when looking for y. And since I'm specifying that I want the first occurrence of x I can also simplify and remove "max".
This gets me what I expect.
A = [0 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 2 1 0]
x = find(A>2,1);
y = find(A(x:end)<3,1);
y = y + (x - 1);
I found a way to remove the math in the last step.
A = [0 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 2 1 0];
x = find(A>2,1);
y = find(A<3);
z = y(find(y>x,1));
And so in my actual code, with me wanting tacos for dinner, this is my implementation...and it seems to work for the data traces which were causing me issues when I posted the question. Find the first instance of my start conditions, dataStart. Find all instances of my end conditions, tacos. Find the first instance within tacos where the element index is greater than my start index.
for ff = 1:length(dataAll)
dataAll(ff).dataStart = find((dataAll(ff).Time > 0 & dataAll(ff).AccActPos > 20 & ...
dataAll(ff).GPSSpeed > 20 & abs(dataAll(ff).SteeringWheelAngle) > 60,1);
tacos = find((dataAll(ff).Time > 0 & dataAll(ff).AccActPos < 20 & ...
dataAll(ff).GPSSpeed > 25 & abs(dataAll(ff).SteeringWheelAngle) < 50));
dataAll(ff).dataEnd = tacos(find(tacos>dataAll(ff).dataStart,1));
end

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Tags

Asked:

on 11 Sep 2020

Edited:

on 14 Sep 2020

Community Treasure Hunt

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

Start Hunting!