首页 > 解决方案 > MATLAB VS Python 索引,IndexError:索引 9654 超出轴 0 的范围,大小为 9654?

问题描述

我正在将 Matlab 代码转换为 python。我收到了这个错误:

a[n_max] = 0
IndexError: index 9654 is out of bounds for axis 0 with size 9654

执行此代码时(n_max 为整数):

for i in range(n_max):
    a[i] = 1/dx2
    c[i] = 1 / dx2
    b[i] = -(2/dx2+np.exp(fi[i])+np.exp(-fi[i]))
    f[i] = np.exp(fi[i]) - np.exp(-fi[i]) - dop[i] - fi[i]*(np.exp(fi[i])+np.exp(-fi[i]))
a[1] = 0
c[1] = 0
b[1] = 1
f[1] = fi[1]
a[n_max] = 0 #error happens here

这是matlab代码:

for i = 1: n_max
    a(i) = 1/dx2;
    c(i) = 1/dx2;
    b(i) = -(2/dx2+exp(fi(i))+exp(-fi(i)));
    f(i) = exp(fi(i)) - exp(-fi(i)) - dop(i) - fi(i)*(exp(fi(i))+exp(-fi(i)));
 end
a(1) = 0;
c(1) = 0;
b(1) = 1;
f(1) = fi(1);
a(n_max) = 0;

我理解错误,所以我尝试通过将最后一行更改为来修复它

 a[n_max-1] = 0 

我不确定我的修改,它是正确的方法吗?有人能解释一下matlab和python之间索引的细微差异吗!

标签: pythonmatlabindexing

解决方案


推荐阅读