0

嗨,我在下面的代码中有 3 维矩阵。我试图对每个“number_panels”和“number_turbines”组合的“n”的所有 24 个元素求和。

我在下面的内容并没有在第三维度上求和

对于 number_panels = 0:5

for number_turbines = 0:2

    for n = 1:24 % number of hours per day



hourly_deficit(number_panels + 1, number_turbines + 1, n) = Demand(n) -...     
(PV_supply(n)*number_panels) - (WT_supply(n)*number_turbines);

if hourly_deficit(number_panels + 1, number_turbines + 1, n)< 0

            hourly_deficit(number_panels + 1, number_turbines + 1, n) = 0;

        end
daily_deficit(number_panels + 1, number_turbines + 1) = sum(sum(sum(hourly_deficit(:,:,:)))) 



        hourly_total_RES(number_panels + 1, number_turbines + 1, n) = PV_supply(n)*number_panels + WT_supply(n)*number_turbines;


        if hourly_total_RES(number_panels + 1, number_turbines + 1, n) < Demand(n),

            renewables_penetration(number_panels + 1, number_turbines + 1, n) = (hourly_total_RES(number_panels + 1, number_turbines + 1, n)) / Demand(n); % ratio of renewable energy supply to demand

        else
            renewables_penetration(number_panels + 1, number_turbines + 1, n) = 1 ;

        end
             peak_deficit(number_panels + 1, number_turbines + 1,n) = max(hourly_deficit(number_panels + 1, number_turbines + 1,n));
    end
4

1 回答 1

1

在 MATLAB sum(X) 中,将矩阵 X 的所有元素一维相加。如果您只想计算一个特定维度的总和,请使用:

 S1=sum(X,dimension);

其中,对于 3D 矩阵,“维度”为 1、2 或 3。

如果你想计算二维,那么你可以这样做:

 S2=sum(S1,dimension);

这里的维度是 1 或 2。

根据您的代码,我认为如果您想在 for 循环中执行此操作,您应该这样做:

for number_panels = 0:5
   for number_turbines = 0:2
      for n = 1:24 % number of hours per day
         hourly_deficit(number_panels + 1, number_turbines + 1, n) =...
           Demand(n) - (PV_supply(n)*number_panels) - (WT_supply(n)*number_turbines);

         if hourly_deficit(number_panels + 1, number_turbines + 1, n)< 0
           hourly_deficit(number_panels + 1, number_turbines + 1, n) = 0;
      end
      daily_deficit(number_panels + 1, number_turbines + 1) = ...
        sum(hourly_deficit(number_panels + 1, number_turbines + 1,:));
    end
 end

查看我添加的“:”: sum(hourly_deficit(number_panels + 1, number_turbines + 1,:));

于 2012-07-14T13:30:17.270 回答