How to select a repetitive range?

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Wolfgang McCormack
Wolfgang McCormack il 22 Mar 2021
Modificato: Wolfgang McCormack il 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 il 1 Set 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

Accedi per commentare.

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by