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

3 views (last 30 days)
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

Alan Stevens
Alan Stevens on 12 Sep 2020
How about
y = find(A(max(x):end)<3);
  3 Comments
Scooby921
Scooby921 on 14 Sep 2020
Edited: Scooby921 on 14 Sep 2020
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)

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!