How to select a repetitive range?

1 view (last 30 days)
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 Comments
Wolfgang McCormack
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
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,:);
>> head(hourly_11_17)
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

Sign in to comment.

Accepted Answer

Walter Roberson
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,:)

More Answers (0)

Categories

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

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!