Storing values in a matrix

6 ビュー (過去 30 日間)
Mohammed Rabani
Mohammed Rabani 2022 年 3 月 9 日
回答済み: Peter Perkins 2022 年 3 月 9 日
Hi,
I have a code which finds the daily average from the hourly values and stores them in a variable. I want to store this values in a matrix so I can use them elsewhere later. Please let me know.
for column = 2:width(cooling_load)
x = table2array(cooling_load(:,column));
for i = 0:364
for j=1:24
sumof =sumof + x(i*24+j+2,1);
end
avgg = sumof/24;
sumof=0;
daily(i+1)=avgg;
end
coolingload = daily'
end
  4 件のコメント
Mohammed Rabani
Mohammed Rabani 2022 年 3 月 9 日
Sorry for the confusion. Idk if this makes more sense but I want to store all the array values into one matrix(or table) instead of individual arrays so I can use them later.

サインインしてコメントする。

採用された回答

Rik
Rik 2022 年 3 月 9 日
You're already there:
for column = 2:width(cooling_load)
x = table2array(cooling_load(:,column));
for i = 0:364
sumof=0;
for j=1:24
sumof =sumof + x(i*24+j+2,1);
end
avgg = sumof/24;
daily(i+1,column-1)=avgg;
% ^^^^^^^^
% just this simple change
end
end

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2022 年 3 月 9 日
This will be MUCH easier using a timetable. It's a one-liner:
>> tt = timetable(rand(100,1),'RowTimes',datetime(2022,3,9,1:100,0,0))
tt =
100×1 timetable
Time Var1
____________________ ________
09-Mar-2022 01:00:00 0.697
09-Mar-2022 02:00:00 0.40754
[snip]
13-Mar-2022 03:00:00 0.089853
13-Mar-2022 04:00:00 0.16103
>> ttDaily = retime(tt,'daily','mean')
ttDaily =
5×1 timetable
Time Var1
___________ _______
09-Mar-2022 0.56102
10-Mar-2022 0.43778
11-Mar-2022 0.49814
12-Mar-2022 0.57098
13-Mar-2022 0.43989
At this point, you can get the means from that daily timetable ...
>> dailyMeans = ttDaily.Var1
dailyMeans =
0.56102
0.43778
0.49814
0.57098
0.43989
... but I doubt you need to. Just leave them there, along with their dates.

Community Treasure Hunt

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

Start Hunting!

Translated by