【问题标题】:matlab for loop in brute forcematlab for循环蛮力
【发布时间】:2012-07-14 06:03:58
【问题描述】:

您好,我正在使用下面代码中显示的蛮力方法。

PV_supply、WT_supply 和 Demand 的大小都是 48x1。

我要做的是分别计算 n = 1:24 和 n = 25:48 的 "hourly_deficit" 方程,以便输出 2 "sets" of "hourly_deficit"

我的代码是

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 循环以获得我正在寻找的结果方面得到一些帮助。就目前而言,for n = 1:24:48 实际上只上升到 n = 24

谢谢

【问题讨论】:

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


【解决方案1】:

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 

【讨论】:

  • 谢谢,因为我正在尝试添加另一个 8760 n,有什么方法可以执行此代码来处理这个大数据集吗?
  • 因为 hourly_deficit 会上升到 hourly_deficit_365?
  • 根据评论编辑。
  • 很好,谢谢!您认为使用 365x24 矩阵比使用 8760x1 矩阵(重新整形)更容易还是实际上相同?
  • 只是为了确认一下——对于“n”小时数 1:24 和“h”小时数 1:24 有一个 for 循环吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
相关资源
最近更新 更多