MATLAB - 为什么这个指数超过矩阵尺寸?

时间:2012-07-28 01:24:02

标签: matlab for-loop multidimensional-array matrix

嗨我有这个代码,它一直给我一个“索引超过矩阵尺寸”的错误。我试图开始循环h = 1,d = 1表示24“h”和3“d”表示“battery_capacity”矩阵= 2,但这似乎与矩阵大小相矛盾。 任何帮助表示赞赏! “h-1”可能导致问题。错误是第二次写入“battery_capacity” 我的代码是      对于idx_number_panels = 1:检查PV面板单元的长度(number_panels)%

for number_turbines = 0:2 % range of wind turbine units examined
  for number_batteries = 1:50 % range of battery units examined
    for h=2:25 %# hours
      for d = 1:number_of_days %# which day
        battery_capacity(idx_number_panels, number_turbines+1, ...
                         number_batteries, 1, 1) =  2*number_batteries;
        %% Charging
        battery_charging(idx_number_panels, number_turbines+1, ...
                         number_batteries, h, d) ...
          = hourly_surplus(idx_number_panels, number_turbines+1, ...
                          number_batteries, h, d) ...
            + battery_capacity(idx_number_panels, number_turbines+1, ...
                               number_batteries, h-1,d);
    end
  end
end

调试器

error line 134 

battery_charging(idx_number_panels, number_turbines+1 ,number_batteries, h,d) =        hourly_surplus(idx_number_panels, number_turbines+1 ,number_batteries, h,d)...
    K>> sz = size(battery_charging)

sz =

 1     1     1     2

K>> index = [idx_number_panels, number_turbines+1 ,number_batteries, h-1,d]

index =

 1     1     1     1     2

K>> ndims(battery_charging)

ans =

  4

在命令行中运行“battery_charging”

>> battery_charging

battery_charging(:,:,1,1) =

 0


battery_charging(:,:,1,2) =

  0
  
    

  

1 个答案:

答案 0 :(得分:2)

比试图找出代码中的问题更容易调试如下。首先,将调试器设置为在出错时中断:

>> dbstop if error

现在,再次运行您的代码。当您遇到错误时,应该会遇到调试提示:

K>>

现在,您可以在发生错误时检查不同索引的值和矩阵的形状,并找出问题的来源。