Home > Enterprise >  Extracting elements from a 1D array. Getting error invalid index to scalar variable
Extracting elements from a 1D array. Getting error invalid index to scalar variable

Time:12-31

I have to extract elements from a 1D array and write a file.

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)

In this I want to extract suppose elements of u0_array separately. I get my u0_array as [[ 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]]. This is just for example. How can I get different elements of u0_array? By using u0[][], I am getting error. Any help is highly appreciated.

CodePudding user response:

u0_array is an array that contains an array of floats. To index an individual element, use u0_array[0][(index to access)]. You can also use .flatten(), as the comment states.

  • Related