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

5 次查看(过去 30 天)
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)?

采纳的回答

Alan Stevens
Alan Stevens 2020-9-12
How about
y = find(A(max(x):end)<3);
  3 个评论
Scooby921
Scooby921 2020-9-14
编辑:Scooby921 2020-9-14
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by