2012-07-14 148 views
0

嗨我在下面的代碼中有三維矩陣。我試圖將每個「number_panels」和「number_turbines」組合的所有24個元素「n」相加。總和三維矩陣 - Matlab蠻力

我具有低於跨過第三尺寸

不總和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 
+0

你是否試圖計算一個3D矩陣的總和? – TJ1 2012-07-14 13:18:57

+0

以及我創建它的方式,我試圖總結「n」維,所以是的,我試圖總結3D矩陣 – user643469 2012-07-14 13:20:02

+0

我希望這可以工作,但它不會:daily_deficit(number_panels + 1,number_turbines + 1)= sum(hourly_deficit(number_panels + 1,number_turbines + 1),3) – user643469 2012-07-14 13:23:11

回答

1

在MATLAB總和(X)增加了矩陣X的所有元素在一個維度。 如果你想只計算一個特定的空間使用的總和:

S1=sum(X,dimension); 

其中「維」是1或2或3的3D矩陣。

如果要計算在兩個維度,那麼你可以這樣做:

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 

看 「:」 我加入到: 總和(hourly_deficit(number_panels + 1,number_turbines + 1,:));

+0

我仍然得到了與您的建議不符的相同答案。 – user643469 2012-07-14 13:32:41

+0

你的X是什麼?它是一個3D矩陣嗎? – TJ1 2012-07-14 13:34:15

+0

看着這個:sum(hourly_deficit(number_panels + 1,number_turbines + 1)),sum()中只有一個元素,sum對你沒有任何作用。你試圖這樣做:sum(sum(hourly_deficit(number_panels + 1:number_turbines + 1))) – TJ1 2012-07-14 13:35:02