Reputation: 35
I have written a code to create a 2-D array H inside a for-loop. Is there any way to store these values of H into another array with n elements so that I can call it later on in my program? I tried using a[i] = H
but I get an error ValueError: setting an array element with a sequence.
import numpy as np
tmin = -30.
tmax = 80.
ntime = 400
Deltat = (tmax-tmin)/ntime
t = np.linspace(tmin,tmax,400)
omega0 = 3.5
tau_s_p = 15
tP = 10
tS = 35
delta_P = 0.5
delta_S = -0.5
omega_P = np.zeros(len(t))
omega_S = np.zeros(len(t))
H_t = np.zeros(len(t))
for n in range(0,len(t)-1):
omega_P[n] = omega0*np.exp((-(t[n]-tP)**2)/(tau_s_p**2))
omega_S[n] = omega0*np.exp((-(t[n]-tS)**2)/(tau_s_p**2))
H = np.array([[0, omega_P[n] , 0], [omega_P[n], 2*delta_P, omega_S[n]],[0,omega_S[n],2*(delta_P - delta_S)]])
Upvotes: 0
Views: 48
Reputation: 4699
You need to copy them element for element. With a[i] = H
you try to store a whole array in an element.
Or you can use the deepcopy
function from the copy
module.
https://stackoverflow.com/a/37593181
Upvotes: 1