# Filtering out rows based on one column and one input

1 view (last 30 days)
Patrick Lonergan on 27 May 2021
Answered: Siddharth Bhutiya on 31 May 2021
Hi,
I am exploring the reasoning behind a given failure. As failures go they are sporadic and spread across a two year period.
I have merged two timetables
1) Timetable of the time of failure (0- no failure 1-failure occurs)
2) Timetable of key measured parameters measured at a 10 minute timestep over two years.
So now I have a timetable with
collumn 1 - Timstep of 10mins
Collumn 2 - Measured Parameter 1
Collumn 3 -Measured Parameter 2
Collumn 4- Measured Pararmeter 3
Collumn 5 - Failure event (0 no failure 1failure )
I am able to filter the rows with failures easily.
But I want to filter the previous 20 rows to each failure, does anyone have any suggestions how this could be best completed?
Ideally after that I would split each failure into a seperate table
Thanks

Siddharth Bhutiya on 31 May 2021
I came up with a possible solution based on your description. I am working with a 100x3 timetable that looks as follows:
Time Param1 Param2 Failure
______ _______ _________ _______
0 min 0.63119 0.78287 0
10 min 0.35507 0.69379 0
20 min 0.997 0.0098023 0
30 min 0.22417 0.84321 0
40 min 1 1 1
50 min 0.60499 0.77095 0
60 min 0.38725 0.04266 0
70 min 0.14219 0.37819 0
Code:
function failureTimetables = extractFailures(tt)
% Get the row indices for failures in the timetable
end
failureIdx = find(tt.Failure == 1);
% Create a cell array to store all the failure sub-timetables
failureTimetables = cell(numel(failureIdx),1);
for i = 1:numel(failureIdx)
% We want to extract 20 rows before a failure but there might not be 20
% rows if our index is less than 20, so for those extract all the rows
% before it.
endIdx = failureIdx(i);
if endIdx <= 20
startIdx = 1;
else
startIdx = endIdx - 20;
end
failureTimetables{i} = tt(startIdx:endIdx,:);
end
This would create a cell array of timetables, one sub-timetable for each failure. Hope this helps.