How to select a repetitive range?

1 次查看(过去 30 天)
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 个评论
Wolfgang McCormack
Wolfgang McCormack 2021-3-22
编辑:Wolfgang McCormack 2021-3-22
@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 2021-9-1
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

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by