matlab for蛮力循环

时间:2012-07-14 19:03:18

标签: matlab optimization for-loop indexing brute-force

嗨,我正在使用我在下面的代码中显示的强力方法。

PV_supply,WT_supply和Demand的大小均为48x1。

我要做的是分别计算n = 1:24和n = 25:48的“hourly_deficit”等式,以输出“hourly_deficit”的2“套”

我的代码是

for number_panels = 0:5
    for number_turbines = 0:3
      for n = 1:24:48 % 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);

end 
 end 
  end

我希望能帮助我调整for循环,以便得到我想要的结果。就目前而言,n = 1:24:48实际上只能达到n = 24

谢谢

1 个答案:

答案 0 :(得分:5)

1:24:48表示“从1到24的增量到48”:如果你保持系列的继续,那么这些值将是1 25 49...。由于49超出了您定义的范围,因此它会停在25

实现您想要的一个解决方案如下:

for n = 1:24 % 1 by 1 to 24

    hourly_deficit_1(...,..., n)= Demand(n)-(PV_supply(n)... %# truncated
    n=n+24;
    hourly_deficit_2(...,..., n)= Demand(n)-(PV_supply(n)... %# truncated

end 

要概括任意天数,请在矩阵中添加第4个维度。第三维是小时(1:24),第四维是白天。

for h=1:24 %# hours
    for d = 1:num_days %# which day
        n = h + 24*(d-1);
        hourly_deficit_1(...,..., h, d)= Demand(n)-(PV_supply(n)... %# truncated
    end    
end 
相关问题