MATLAB Answers

Matrix addition hour values to day values

35 views (last 30 days)
Hello guys it is my first question in here so you can imagine I'm not that good at matlab.
I got a table with 8760 lines (beta). Every value is an hour value and I wanted to sum every 24 lines to one day if you know what i mean.
So i need a loop or a possibility without "sum" command which can count 24 lines together and go to the next 24.
The results have to be a new matrix with 365 lines (so one year).
My code doesnt work but maybe you can help.
Its my first semester informatics and I've been trying to solve it for days.
n = 1;
N = 24;
for i = b:c;
for v = 1:8760
Beta(1,v) = beta(n,N);
n = n + 24
N = N + 24
end
end

  0 Comments

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 4 Jan 2021
Edited: Cris LaPierre on 4 Jan 2021
If I were going to do this, I would create a vector of datetimes incremented by 1 hour. I would then create a table with the datetimes and beta. I would then use groupsummary to calculate the sum of each day.
Here's a rough example.
beta = rand(8760,1);
d = datetime(2020,1,1,0,0,0)+hours(0:length(beta)-1)';
data = table(d,beta)
data = 8760x2 table
d beta ____________________ _______ 01-Jan-2020 00:00:00 0.84722 01-Jan-2020 01:00:00 0.19304 01-Jan-2020 02:00:00 0.40865 01-Jan-2020 03:00:00 0.88989 01-Jan-2020 04:00:00 0.85154 01-Jan-2020 05:00:00 0.53655 01-Jan-2020 06:00:00 0.18732 01-Jan-2020 07:00:00 0.60993 01-Jan-2020 08:00:00 0.76008 01-Jan-2020 09:00:00 0.21881 01-Jan-2020 10:00:00 0.28571 01-Jan-2020 11:00:00 0.85716 01-Jan-2020 12:00:00 0.23839 01-Jan-2020 13:00:00 0.85945 01-Jan-2020 14:00:00 0.30243 01-Jan-2020 15:00:00 0.2486
dailySum = groupsummary(data,'d','dayofyear','sum')
dailySum = 365x3 table
dayofyear_d GroupCount sum_beta ___________ __________ ________ 1 24 10.841 2 24 11.897 3 24 14.002 4 24 11.924 5 24 12.63 6 24 12.117 7 24 10.108 8 24 12.824 9 24 12.373 10 24 12.469 11 24 11.382 12 24 13.871 13 24 10.718 14 24 13.249 15 24 12.944 16 24 14.145

More Answers (1)

VBBV
VBBV on 4 Jan 2021
Edited: VBBV on 4 Jan 2021
clc
clearvars
n = 1;
beta = rand(8760,1);
x = 0;
v = 1;
F = zeros(365,1);
for k = 1:365
while v <= 24*k
x = x + beta(v,1);
v = v+1;
end
F(k) = x;
x = 0;
end
Try this

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by