python中的“索引1超出了轴0的大小为1的范围”

时间:2019-05-30 15:55:06

标签: python numpy indexing

我似乎有索引问题?我不知道如何解释这个错误...:/我认为这与我如何初始化u有关。

我有一个使用变量u(向量,x-y)创建的3x3 G矩阵。我现在只是制作了一个零矩阵,因为我还不太确定如何编写它,因为其中涉及很多局部和规范。 x_j =(x_1(j),x_2(j),x_3(j))和y_j =(y_1(j),y_2(j),y_3(j))。 x和y是nx3个向量。 alpha_j是3x3矩阵。 A矩阵是大小为3nx3n的块对角矩阵。我在W矩阵(大小为3nx3n,其中第(i,j)个块是由alpha_i * G_ [ij] * alpha_j给出的3x3矩阵)上遇到麻烦。

def G(u):

    u1 = u[0]
    u2 = u[1]
    u3 = u[2]
    g = np.array([[0,0,0],[0,0,0],[0,0,0]],complex)  

    return g

def W(x, y, k, alpha, A):

    # initialization
    n = x.shape[0] # the number of x vextors 
    result = np.zeros([3*n,3*n],complex)
    u = np.matlib.zeros((n, 3)) # u = x - y 
    print(u)
    num_in_blocks = n

    # variables
    a_i = alpha_j(alpha, A)
    a_j = alpha_j(alpha, A)

    for i in range(0, 2):
        x1 = x[i] # each row of x
        y1 = y[i] # each row of y
        for j in range(0, n-1):
            u[i][j] = x1[j] - y1[j] # each row of x minus each row of y
        if i != j:
            block_result = a_i * G((u[i][j]), k) * a_j
            for k in range(num_in_blocks):
                for l in range(num_in_blocks):
                    result[3*i + k, 3*j + l] = block_result[i, j] 

    return result

def alpha_j(a, A):
    alph = np.array([[0,0,0],[0,0,0],[0,0,0]],complex)
    n = A.shape[0]
    rho = np.random.rand(n,1)
    for i in range(0, n-1):
        for j in range(0, n-1):
            alph[i,j] = (rho[i] * a * A[i,j])
    return alph

#------------------------------------------------------------------

# random case

def x(n):
    return np.random.randint(100, size=(n, 3))

def y(n):
    return np.random.randint(100, size=(n, 3))

# SYSTEM PARAMETERS

theta = 0 # can range from [0, 2pi)

chi = 10 + 1j

lam = 0.5 # microns (values between .4-.7)

k = (2 * np.pi)/lam # 1/microns

V_0 = (0.05)**3 # microns^3

K = k * np.array([[0], [np.sin(theta)], [np.cos(theta)]])

alpha = (V_0 * 3 * chi)/(chi + 3)

A = np.matlib.identity(3) 

#------------------------------------------------------------------

# TEST FUNCTIONS

w = W(x(3), y(3), k, alpha, A)
print(w)

我一直收到错误“标量变量的无效索引”。在u1 = u [0]行。

1 个答案:

答案 0 :(得分:0)

np.matlib构成np.matrix的子类np.ndarray。它应该给人一种MATLAB的感觉,并且(几乎)总是产生2d数组。不鼓励在新代码中使用它。

In [42]: U = np.matrix(np.arange(9).reshape(3,3))                                    
In [43]: U                                                                           
Out[43]: 
matrix([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])

使用[0]进行索引会选择第一行,但会返回2d矩阵。

In [44]: U[0]                                                                        
Out[44]: matrix([[0, 1, 2]])
In [45]: U[0].shape                                                                  
Out[45]: (1, 3)

添加另一个[1]仍会索引第一个维度(现在为大小1):

In [46]: U[0][1]                                                                     
---------------------------------------------------------------------------
IndexError: index 1 is out of bounds for axis 0 with size 1

通常我们用复合索引为numpy数组建立索引:

In [47]: U[0,1]                                                                      
Out[47]: 1

如果我们改用ndarray

In [48]: U = np.arange(9).reshape(3,3)                                               
In [49]: U                                                                           
Out[49]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In [50]: U[0]                                                                        
Out[50]: array([0, 1, 2])      # 1d
In [51]: U[0][1]               # works,                                                       
Out[51]: 1
In [52]: U[0,1]                # still preferable                                                      
Out[52]: 1