How can I easily extract element from a big matrix?

1 visualizzazione (ultimi 30 giorni)
I have a bigg matrix [365x24] whic represtens a specific property for each hour and day of the year.
It can be read like this:
The first line has of 24 elements whit elements whic are corresponding to the 00:00 to 23:00 of Monday; the second is Tusday and so on with the Seventh that is Sunday and then it restart with the following weekt.
The first 31 row represent the days of January the following 28 rows rapresents the days of february and so on...
I would like to extrat some specific intervals of data for each month and for the whole year, that are
F1--the elements fom 8:00- to 19:00 (8 am to 7 pm) for each moday to friday interval
F2--the elements and 20:00 to 23:00 for each Monday to Friday interval and Saturday all day from 0:00 to 23
F3-- The elements from 00:00 to 7:00 (midnight -7 am) for each Monday to Saturday interval; and al Sundays all day fro 00:00 to 23
The intervals are summarized in figure
I tried to do it mannualy but it's a messy and slow method, how can i do it quickly?
%For example for F1
A=randi(365,24);
F1m=[A(1:5,8:19);A(8:12,8:19);A(15:19,8:19); A(22:26,8:19);A(29:33,8:19); A(36:40,8:19);
A(43:47,8:19);A(50:54,8:19);A(57:61,8:19);A(CYear(64:68,8:19);A(71:75,8:19); A(78:82,8:19)... ];
%and the sum
F1=sum(sum(F1m));
  9 Commenti
Adam Danz
Adam Danz il 18 Lug 2022
I assume you have a 1x24 vector that defines the time for each column or at least you the time of the first column and the duration of each column which could be used to compute the 1x24 time vector.
I also assumes you have a 365x1 vector of dates that define each row or a starting date and the duration of each row that could be used to compute the date vector.
Once you have those values, you could create a timetable as @dpb suggested above. Once you have a timetable, your task becomes much simpler.
Pietro Fiondella
Pietro Fiondella il 18 Lug 2022
No i havent but i could create them.,But i dont know what i shlod do with them then.
Can someone make an example with an easier matrix idk [5x5]? I will accept it as an anwser.

Accedi per commentare.

Risposta accettata

dpb
dpb il 18 Lug 2022
Modificato: dpb il 19 Lug 2022
Yeah, I'd convert to timetable and reorganize -- something like this
Time=datetime(2022,1,1,[0:365*24-1].',0,0); % create the time vector hourly; arbitrary nonleapyear
M=readmatrix('Matrix.xlsx').'; % read data, arrange by hour
ttM=timetable(Time,M(:),'VariableNames',{'V'}); % create base timetable
ttM.Time.Format='HH'; % just show the hours
Class=categorical("F"+[1:3].'); % your class grouping variable
ttM=addvars(ttM,repmat(Class(3),height(ttM),1),'Before','V','NewVariableNames','Class'); % Class 3 is largest
isF2=isbetween(timeofday(ttM.Time),hours(7),hours(23))&(~matches(day(ttM.Time,'name'),'Sunday')); % next subset
ttM.Class(isF2)=Class(2);
isF1=isbetween(timeofday(ttM.Time),hours(8),hours(19))&~isweekend(ttM.Time); % smallest
ttM.Class(isF1)=Class(1);
The above gives
>> head(ttM,10)
ans =
10×2 timetable
Time Class V
____ _____ _______
00 F3 0.70825
01 F3 0.74999
02 F3 0.79296
03 F3 0.85226
04 F3 0.9795
05 F3 0.99869
06 F3 1.207
07 F2 1.4139
08 F2 1.6649
09 F2 1.6355
>>
Now use the Class variable as the grouping variable and the rest is piece o' cake w/ retime() or rowfun() or grpstats() or ...
I'll leave as "exercise for Student" adding DOW names and weekend or whatever other pretty variables care to add...
  3 Commenti
dpb
dpb il 18 Lug 2022
Turned your data on its head as linear vector versus time and created the time vector to go with it explicitly instead of trying to infer something by position.
Study the sections in the doc on tables and timetables to begin to get an idea of their flexibiliity and power -- don't overlook the 'Functions' link at the top of the specific doc pages -- it has a comprehensive list of all the functions tailored specifically for manipulating the data class...and, of course, the section on addressing data in a table is indispensible.
dpb
dpb il 18 Lug 2022
>> groupsummary(ttM,'Class','sum')
ans =
3×3 table
Class GroupCount sum_V
_____ __________ ______
F1 3120 4928
F2 2201 3033.5
F3 3439 3434.5
>>

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by