sum every 24 rows in a vector

59 ビュー (過去 30 日間)
Andrew Alkiviades
Andrew Alkiviades 2012 年 9 月 24 日
コメント済み: Mohamed Atef 2019 年 1 月 19 日
Hi I am trying to find an output which is the sum of every 24 rows of a 8760x1 vector below as hourly_deficit. Therefore I am trying to sum rows 1:24, 25:49, 50:73 etc etc
I am trying to do this on the line below
for idx_number_panels = 1:length(number_panels) % range of PV panel units examined
for idx_number_turbines = 1:length(number_turbines) % range of wind turbine units examined
for idx_number_batteries = 1:length(number_batteries) % range of battery units examined
for h=2:8759 %# hours
hourly_deficit(idx_number_panels,idx_number_turbines,idx_number_batteries, h) = hourly_annual_demand(h) - (hourly_annual_PV(h)*number_panels(idx_number_panels)) - (hourly_annual_WT(h)*number_turbines(idx_number_turbines));

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 24 日
編集済み: Matt Fig 2012 年 9 月 24 日
If A is 8760-by-1, and you want to find the sum of every 24 elements, such that you will end up with 365 sums, then do:
sum(reshape(A,24,365))
As an example you can see easier, get the sum of every two elements of a 10-by-1:
A = (1:10)'
B = reshape(A,2,5)
sum(B)
  1 件のコメント
Mohamed Atef
Mohamed Atef 2019 年 1 月 19 日
Thanks alot man

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

その他の回答 (2 件)

Daniel Shub
Daniel Shub 2012 年 9 月 24 日
編集済み: Daniel Shub 2012 年 9 月 24 日
I am not sure what all the code you posted has to do with anything ...
If I have a 8760x1 array
x = randn(8760, 1);
I can reshape it to be 24x365 with
y = reshape(x, 24, 365);
and then sum each of the 365 columns
z = sum(y);
EDIT
You could also filter the data
z = filter(ones(24, 1), 1, x);
z = z(24:24:end);

Honglei Chen
Honglei Chen 2012 年 9 月 24 日
編集済み: Honglei Chen 2012 年 9 月 24 日
Not sure what your end format is, but the following code adds every 24 rows and retain all the results in one column
reshape(sum(reshape(x,24,[])),[],1)

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by