python - 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之间索引的细微差异吗!
解决方案
推荐阅读
- express - res.render 传递本地参数的正确方法
- iis - 如何在浏览器中重写 url 但指向现有内容
- flutter - Flutter IFrameElement 不会更改源 url
- laravel - Laravel/VueJS 项目 - 控制台错误未显示错误所在的位置
- java - Windows 长路径作为当前目录
- django - 无法将数据库(带有图像)集成到 Django 项目中
- javascript - 导入 package.json 时文件不打包
- pine-script - 如何在我的 TradingView 策略中实施交易费用计算,以便我可以告诉卖出警报仅在 > 费用和利润时触发
- angular - Angular:在 localhost 上使用 npm start 在自定义上下文根上提供应用程序
- amazon-web-services - 错误:启动源实例时出错:不支持:此实例类型不支持请求的租赁