0

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)]])


2
  • You don't show the whole code. What's a? Commented Dec 14, 2020 at 16:11
  • @hpaulj a is just an array I initialize to store the values of H for each iteration. Commented Dec 15, 2020 at 1:28

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I tried element by element but that was too long so I was trying to make my code more compact. The deepcopy function works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.