从一维数组中提取元素。获取标量变量的错误索引无效
Extracting elements from a 1D array. Getting error invalid index to scalar variable
我必须从一维数组中提取元素并写入文件。
import numpy as np
k0 =78
tpts = 10
x_mirror = [1,2,3,4,5,6,7,8,9,10]
alpha = -3
x_start = 3
u0 = []
for p in range(0,tpts): #initial condition at t = 0
ts = ((np.exp(alpha*((x_mirror[p]-x_start)**2))*(np.cos(k0*(x_mirror[p]-x_start)))))
u0.append(ts)
u0_array = np.array(u0)[np.newaxis]
u0_array_transpose = np.transpose(u0_array)
matrix_A = np.zeros((tpts,tpts))
matrix_A[tpts-1][tpts-1] = 56
matrixC = matrix_A @ u0_array_transpose
matrixC2 = matrix_A @ u0
u_pre = np.array(np.zeros(tpts))
print(u0_array)
在此我想分别提取 u0_array 的假定元素。我得到 u0_array 作为 [[ 2.89793185e-06 -4.27075012e-02 1.00000000e+00 -4.27075012e-02 2.89793185e-06 9.14080657e-14 -7.91091805e-22 2.42062877e-33 -1.24204313e-47 1.15841796e-64]]
。这只是举例。如何获取 u0_array 的不同元素?通过使用 u0[][],我收到了错误。非常感谢任何帮助。
u0_array
是一个包含浮点数组的数组。要索引单个元素,请使用 u0_array[0][(index to access)]
。您还可以使用 .flatten()
,如评论所述。
我必须从一维数组中提取元素并写入文件。
import numpy as np
k0 =78
tpts = 10
x_mirror = [1,2,3,4,5,6,7,8,9,10]
alpha = -3
x_start = 3
u0 = []
for p in range(0,tpts): #initial condition at t = 0
ts = ((np.exp(alpha*((x_mirror[p]-x_start)**2))*(np.cos(k0*(x_mirror[p]-x_start)))))
u0.append(ts)
u0_array = np.array(u0)[np.newaxis]
u0_array_transpose = np.transpose(u0_array)
matrix_A = np.zeros((tpts,tpts))
matrix_A[tpts-1][tpts-1] = 56
matrixC = matrix_A @ u0_array_transpose
matrixC2 = matrix_A @ u0
u_pre = np.array(np.zeros(tpts))
print(u0_array)
在此我想分别提取 u0_array 的假定元素。我得到 u0_array 作为 [[ 2.89793185e-06 -4.27075012e-02 1.00000000e+00 -4.27075012e-02 2.89793185e-06 9.14080657e-14 -7.91091805e-22 2.42062877e-33 -1.24204313e-47 1.15841796e-64]]
。这只是举例。如何获取 u0_array 的不同元素?通过使用 u0[][],我收到了错误。非常感谢任何帮助。
u0_array
是一个包含浮点数组的数组。要索引单个元素,请使用 u0_array[0][(index to access)]
。您还可以使用 .flatten()
,如评论所述。