我必須從一維陣列中提取元素并寫入一個檔案。
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[][],我得到了錯誤。任何幫助都受到高度贊賞。
uj5u.com熱心網友回復:
u0_array是一個包含浮點陣列的陣列。要索引單個元素,請使用u0_array[0][(index to access)]. 您也可以使用.flatten(), 如評論所述。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/400544.html
