我正在尝试更改此Matlab for循环:
n =100;
u_lsg = zeros (n ,n) ;
for i =1:1: n
for j =1:1: n
u_lsg(i , j) =( i*h)*(j*h )*(1 -( i*h ))*(1 -( j*h ));
end
end放入python循环中。
到目前为止,我得到的是:
n =100
u_lsg = np.zeros((n ,n))
for i in range (1,n+1,1):
for j in range (1,n+1,1):
u_lsg[i,j]= (i*h)*(j*h)*(1-(i*h))*(1-(j*h))但这给了IndexError: index 100 is out of bounds for axis 1 with size 100。
我做错什么了?据我所知,python中没有end命令,但是如何结束循环呢?
发布于 2021-07-31 15:29:21
Matlab是1-indexed (数组索引从1开始),Python (和NumPy)是0-indexed (数组索引从0开始),所以只需相应地更改范围:
n = 100
u_lsg = np.zeros((n, n))
for i in range(n):
for j in range(n):
u_lsg[i, j] = (i*h)*(j*h)*(1-(i*h))*(1-(j*h))https://stackoverflow.com/questions/68603444
复制相似问题