First occurance of data in timetable

8 visualizaciones (últimos 30 días)
Sarvesh
Sarvesh el 4 de En. de 2024
Comentada: Sarvesh el 5 de En. de 2024
Hi and thanks in advance.
I have a timetable with windspeed data collected 6 hrly on different days of the month for the past 20 years (not continous). The timetable has 3 columns (date/time, ID, windspeed) for each ID there are 5-20 data points. For each ID I want to find the first occurance of windspeed > 17.5 and delete everything above. Simialry I want to find the last occurnace of windspeed > 17.5 and delete everything below.
FOr example, for the ID 3456, windspeed values are 10 12 18 16 12 20 15
After analysis i want the values as 18 16 12 20 along with the timestamps.
Thanks Once Again

Respuesta aceptada

Rishi
Rishi el 4 de En. de 2024
Hi Sarvesh,
I understand that you want to write a code such that it gets rid of the occurrences which have windspeeds less than 17.5 before and after the first and last occurrences of windspeed greater than 17.5, respectively, for every ID.
Here is the code for the same:
% Assuming 'tt' is your timetable with variables 'DateTime', 'ID', and 'Windspeed'
DateTime = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13';'2015-12-18 12:03:15';'2015-12-18 13:03:05';'2015-12-18 14:03:05';'2015-12-18 15:03:05';'2015-12-18 16:03:05';'2015-12-18 17:03:05';'2015-12-18 18:03:05'});
ID = [1; 2; 1; 2; 1; 2; 1; 1; 2; 2];
Windspeed = [10; 19; 18; 11; 16; 22; 20; 15; 13; 11];
tt = timetable(DateTime,ID,Windspeed);
uniqueIDs = unique(tt.ID);
trimmedTimetable = timetable();
for i = 1:length(uniqueIDs)
currentSubset = tt(tt.ID == uniqueIDs(i), :);
startIndex = find(currentSubset.Windspeed > 17.5, 1, 'first');
endIndex = find(currentSubset.Windspeed > 17.5, 1, 'last');
if ~isempty(startIndex) && ~isempty(endIndex)
currentSubset = currentSubset(startIndex:endIndex, :);
trimmedTimetable = [trimmedTimetable; currentSubset];
end
end
% Sort the trimmed timetable by DateTime if necessary
trimmedTimetable = sortrows(trimmedTimetable, 'DateTime');
disp(trimmedTimetable);
Hope this helps!
  1 comentario
Sarvesh
Sarvesh el 5 de En. de 2024
thanks Rishi. This is exactly what i needed

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by