First occurance of data in timetable

조회 수: 2 (최근 30일)
Sarvesh
Sarvesh 2024년 1월 4일
댓글: Sarvesh 2024년 1월 5일
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

채택된 답변

Rishi
Rishi 2024년 1월 4일
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
Sarvesh
Sarvesh 2024년 1월 5일
thanks Rishi. This is exactly what i needed

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by