Improve part of code using a loop
1 view (last 30 days)
Show older comments
I have an annual table X, where I want to calculate the five maximum values for each month. My code works but I need to also apply it for other variables so I am wondering how to shrink it a bit using a loop. So far i splitted the initial annual table (X) into months like this:
X_January = table(Dates{1,1},X{1,1});
X_February = table(Dates{2,1},X{2,1});
X_March = table(Dates{3,1},X{3,1});
X_April = table(Dates{4,1},X{4,1});
X_May = table(Dates{5,1},X{5,1});
X_June = table(Dates{6,1},X{6,1});
X_July = table(Dates{7,1},X{7,1});
X_August = table(Dates{8,1},X{8,1});
X_September = table(Dates{9,1},X{9,1});
X_October = table(Dates{10,1},X{10,1});
X_November = table(Dates{11,1},X{11,1});
X_December = table(Dates{12,1},X{12,1});
%Max 5 values for January
[Col,idx] = maxk(X_January{:,1},5);
T= X_January(idx,:)
Any ideas how to make a loop?
2 Comments
Dyuman Joshi
on 4 Feb 2023
Edited: Dyuman Joshi
on 4 Feb 2023
How do you want to store your final output?
All values in a numeric array (12x5 or 5x12 )? If otherwise, please specify.
Accepted Answer
Star Strider
on 4 Feb 2023
One approach —
date_time = (datetime('01-Jan-2023') : caldays(1) : datetime('31-Dec-2023')).';
Values = randn(numel(date_time), 5);
T1 = [table(date_time) array2table(Values)]
for k = 1:12
Lv = month(T1.date_time) == k;
M = T1(Lv,:);
[~,idx] = maxk(M{:,2},5); % Index Returns 5 Highest Values Of First Variable
Month{k} = M(idx,:);
end
Month{1} % January
Month{12} % December
Please do not use numbered variables! Simply store the results in an array.
.
14 Comments
Star Strider
on 23 Feb 2023
As always, my pleasure!
The easiest way to find out is to do the experiment, adding:
Month{k} = [Month{k}; []];
That will not throw an error, however it does not affect the result, and neither does:
Month{k} = [Month{k}; cell(1,size(Month{k},2))];
while this works:
Month{k} = [Month{k}; table(NaT,NaN,NaN,NaN, 'VariableNames',Month{k}.Properties.VariableNames)];
however this (and analogues of it) do not:
Month{k} = [Month{k}; table({},[],[],[], 'VariableNames',Month{k}.Properties.VariableNames)];
So it appears not to be possible.
I will leave this up here so you can experiment with it —
date_time = (datetime('01-Jan-2023') : caldays(1) : datetime('31-Dec-2023')).';
Temperature = randn(numel(date_time), 1);
T1 = [table(date_time) array2table(Temperature)]
VN = T1.Properties.VariableNames;
[Y,M,D] = ymd(T1.date_time);
DN = day(T1.date_time,'dayofyear');
T1 = addvars(T1,M,Y,'AFter','date_time');
T1.Properties.VariableNames = {VN{1},'M','Y',VN{2}}; % Necessary, Since They Show Up As 'Var2' etc. Otherwise
T1
for k = 1:12
Lv = month(T1.date_time) == k;
M = T1(Lv,:);
[~,idx] = maxk(M{:,2},5); % Index Returns 5 Highest Values Of First Variable
Month{k} = M(idx,:);
% Q = cell(1,size(Month{k},2))
Month{k} = [Month{k}; table({},[],[],[], 'VariableNames',Month{k}.Properties.VariableNames)];
% Month{k} = [Month{k}; table(NaT,NaN,NaN,NaN, 'VariableNames',Month{k}.Properties.VariableNames)];
end
Month{1} % January
Month{12} % December
.
More Answers (0)
See Also
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!