五维矩阵操作

时间:2012-07-26 17:16:34

标签: matlab for-loop matrix

我对代码进行了以下更改,但仍然在调用“if语句”的行上得到“索引超出矩阵维度”错误,而我用于从2:25开始循环“h”。我仍然有网想出如何在当前维度方程表达式中使用前一维度的元素

  number_of_days = 3;
number_of_hours = 24*number_of_days;
number_panels = 1:5;


for idx_number_panels = 1:length(number_panels) % range of PV panel units examined

for number_turbines = 0:1 % range of wind turbine units examined

    for number_batteries = 1:2 % range of battery units examined


        for h=2:25 %# hours
               battery_capacity(:,:,:,1,1) = max_battery_capacity*number_batteries;

            for d = 1:number_of_days %# which day

                n = h + 24*(d-1);



                if   (max_battery_capacity*number_batteries) - (battery_capacity(idx_number_panels, number_turbines+1 ,number_batteries, h-1,d)*number_batteries) >0

                    storage_availability(idx_number_panels, number_turbines+1 ,number_batteries, h,d) =  (max_battery_capacity*number_batteries) - (battery_capacity(idx_number_panels, number_turbines+1 ,number_batteries, h-1,d)) ;

                else


                    storage_availability(idx_number_panels, number_turbines+1 ,number_batteries, h,d) = 0;


                end

2 个答案:

答案 0 :(得分:2)

让我们看几个小时。

for h = 1:24
    battery_capacity(1) = initial_battery_capacity*number_batteries

    if hourly_total_RES(h) > hourly_annual_demand(n), % battery charging
        battery_capacity(h) = battery_capacity(h-1);
    else
        battery_capacity(h) = battery_capacity(h-1);
    end
end

首先,if语句的两面与写入的相同。我假设您的实际代码使用以前的数据进行某种工作。如果没有,这是一个问题。

如果切换日期和小时循环的顺序,它也可能使代码更容易思考。对我来说,一次查看一天中的所有时间比看每天的第一个小时,然后是每天的第二个小时更有意义......

对于索引,一个明确的错误是你在循环的第一次迭代中索引battery_capacity(h-1)。也就是说,当h为1时,您定义battery_capacity(1),然后尝试查看battery_capacity(0),这可能是错误的原因。

要解决此问题,您可以查看是否h == 1,但我认为更优雅的方法是在进入该循环之前循环h = 2:24并设置battery_capacity(1)。看看这段代码是否有效:

for d = 1:number_of_days
    battery_capacity(1) = initial_battery_capacity*number_batteries
    for h = 2:24
        if hourly_total_RES(h) > hourly_annual_demand(n), % battery charging
            battery_capacity(h) = battery_capacity(h-1);
        else
            battery_capacity(h) = battery_capacity(h-1);
        end
    end
end

答案 1 :(得分:0)

根据我的理解,最后两个维度分别存储小时和天。所以要设置第一天的值在小时= 1(我假设这意味着午夜开始):

battery_capacity(:,:,:,1,1) = 2;    %# 2kWh

这将为所有“面板”和所有“涡轮机”以及所有“电池”设置值2

我假设您已经在代码中的某处预先分配了矩阵。


对于它的价值,我认为您在代码中首次提到battery_capacity时会出现错字(缺少h参数)