和三维矩阵 - Matlab蛮力

时间:2012-07-14 13:11:55

标签: matlab optimization for-loop brute-force

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

我在下面的内容并未总结第三维

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(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

1 个答案:

答案 0 :(得分:1)

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

 S1=sum(X,dimension);

其中“dimension”为1,或2或3表示3D矩阵。

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

 S2=sum(S1,dimension);

其中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,:));