How to select a repetitive range?

2 views (last 30 days)
Wolfgang McCormack on 22 Mar 2021
Commented: Peter Perkins on 1 Sep 2021
Hi all,
I have a data from 1 to 8760 representing the hours of the year. I want to select for instance 11 to 17 and repeat it every 24 hours (selecting the same range in the next day, the value will be added by 24 so that it jumps to 11 o'clock in the second day and so on). How should I do that?
Thanks
3 CommentsShow 1 older commentHide 1 older comment
Wolfgang McCormack on 22 Mar 2021
Edited: Wolfgang McCormack on 22 Mar 2021
@dpb well that's more lines of code to get than done since I have to create a conditional time selection for hours. I wish matlab could handle that automatically in timetables.
Peter Perkins on 1 Sep 2021
Actually, this is super easy using a timetable:
>> hourly = timetable(datetime(2021,1,1,0:8759,0,0)',rand(8760,1));
>> t = datetime(2021,1,1:365) + hours(11:17)'; % implicit expansion: row + column
>> hourly_11_17 = hourly(t,:);
ans =
8×1 timetable
Time Var1
____________________ ________
01-Jan-2021 11:00:00 0.61605
01-Jan-2021 12:00:00 0.89185
01-Jan-2021 13:00:00 0.71641
01-Jan-2021 14:00:00 0.86522
01-Jan-2021 15:00:00 0.83825
01-Jan-2021 16:00:00 0.019567
01-Jan-2021 17:00:00 0.33124
02-Jan-2021 11:00:00 0.38599

Walter Roberson on 22 Mar 2021
data_day = reshape(YourData, 24, [], size(YourData,2));
data_day(11:17, :, :)
You could reshape() that to [], size(data_day,3) if you want to push the chunks back together again, but I suspect having it by hour will be more useful.
Or
idx = (11:17).' + (0:24:size(YourData,1)-1);
selected_data = YourData(idx,:)

Categories

Find more on Dates and Time in Help Center and File Exchange

R2020b

Community Treasure Hunt

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

Start Hunting!