嵌套FOR循环索引问题-Python

嵌套FOR循环索引问题-Python,python,for-loop,numpy,indexing,Python,For Loop,Numpy,Indexing,我的索引在嵌套for循环中遇到问题。Python抛出了一个索引错误,告诉我我的索引超出了范围 以下是我的代码和后续错误: from math import * import numpy as np from scipy import integrate import matplotlib.pyplot as plt import os croot = 1 ctip = 1 span = 1 thetaroot = 0 thetatip = 0 a0root = 0.11 a0tip = 0.1

我的索引在嵌套for循环中遇到问题。Python抛出了一个索引错误,告诉我我的索引超出了范围

以下是我的代码和后续错误:

from math import *
import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
import os

croot = 1
ctip = 1
span = 1
thetaroot = 0
thetatip = 0
a0root = 0.11
a0tip = 0.11
alpha = 0
alpha0root = -2.5
alpha0tip = -2.5
thetaroot = thetaroot * arctan(1.) / 45.
thetatip = thetatip * arctan(1.) / 45.
alpha = alpha * arctan(1.) / 45.
alpha0root = alpha0root * arctan(1.) / 45.
alpha0tip = alpha0tip * arctan(1.) / 45.
n = 10
theta = np.empty(n, dtype = object)
y = np.empty(n, dtype = object)
c = np.empty(n, dtype = object)
cl = np.empty(n, dtype = object)
alp = np.empty(n, dtype = object)
a = np.empty(n, dtype = object)
rhs = np.empty(n, dtype = object)
b = np.empty(n, dtype = object)
a = np.empty(n, dtype = object)
rhs = rhs[:,None]
b = b[:,None]
a = a[:,None]
#
# Define properties at n span stations
#
pi = 4. * arctan(1.)
for i in range(0,n):
    theta[i] = i * pi / (2. * n)
    y[i] = span * 0.5 * cos(theta[i])
    c[i] = croot + (ctip - croot) * y[i] * 2. / span
    alp[i] = alpha + thetaroot - (alpha0root + (alpha0tip - alpha0root + thetaroot - thetatip) * y[i] * 2. / span)
    a[i] = a0root + (a0tip - a0root) * y[i] * 2. / span

pi = 4. * arctan(1.)
# Set up 2n x 2n system of equations for A1, A3 , ... A2n-1
for j in range(0,n):
    mu = c[j] * a[j] / (4. * span); print('mu=',mu)
    rhs[j,0] = alp[j] * sin(theta[j]) * c[j] * a[j] / (4 * span)
    for i in range(0,n):
        l = 2 * i - 1
        b[j,i] = sin(l * theta[j]) * (mu * l + sin(theta[j]))
然后我收到错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-70-b5bd95e69bb5> in <module>()
     45     for i in range(0,n):
     46         l = 2 * i - 1
---> 47         b[j,i] = sin(l * theta[j]) * (mu * l + sin(theta[j]))
     48 
     49 

IndexError: index 1 is out of bounds for axis 1 with size 1
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在()
范围(0,n)内的i为45:
46L=2*i-1
--->47 b[j,i]=sin(l*θ[j])*(mu*l+sin(θ[j]))
48
49
索引器:索引1超出大小为1的轴1的界限
如何有效地调用这两个索引?在MATLAB中,b(j,i)是正常语法


感谢您的帮助,谢谢

调用
b=np.empty(n,dtype=object)
with
n=10
生成一个一维数组,但您正在对它进行索引(
b[j,i]
),就好像它是一个二维数组一样

要初始化一个10乘10的数组,可以调用
b=np.empty([n,n],dtype=object)

编辑: 我没有注意到这个作业:
b=b[:,无]
这就产生了:

>>> [[None]
 [None]
 [None]
 [None]
 [None]
 [None]
 [None]
 [None]
 [None]
 [None]]

这是一个二维数组,但试图索引内部数组的第一个元素(不包含任何元素)会导致您的错误。

这是numpy吗?在这种情况下,您在哪里导入相关的LibrarySapologies,我已使用导入的库更新了我的脚本。正在使用Numpy。
b
(n,1)
。如果类似的东西在MATLAB中起作用,那是因为如果索引超出当前大小,该语言会扩展数组。在
numpy
中,您必须将其初始化为正确的最终形状。我假设您的意思是b=np.empty([n,n],dtype=object)?哦,是的,我的意思是。因此,由于它试图索引第一个元素,您会建议如何作为嵌套循环的可行替代方案?我的建议是创建b as
b=np.empty([n,n],dtype=object)
,则嵌套循环应该可以工作。(并删除
b=b[:,None]
)这样做时,我收到一个错误,指出“索引太多”。它指向包含rhs[j,i]=alp[j]*sin(θ[j])*c[j]*a[j]/(4*span)的线