Mokus
Mokus

Reputation: 10400

How can I put 2d array to 2d array in numpy?

I have the following code

a_alpha_beta = zeros((2,  len( atime ) ))

for i in range( len( atime ) ):        
        alpha_beta = transf.clarke(signal[0][i], signal[1][i], signal[2][i])
        a_alpha_beta[0][i] = alpha_beta[0][0] 
        a_alpha_beta[1][i] = alpha_beta[1][0]

How can I optimize the code above, for example how can I copy alpha_beta to a_alpha_beta?

Upvotes: 0

Views: 84

Answers (1)

Taro Sato
Taro Sato

Reputation: 1452

I don't exactly know what the function transf.clarke does, but copy operations that you desire can be done as follows:

import numpy as np

# generate a test input
x = np.arange(0, 10).reshape((2, 5))
print x

# simply copy 2d array
y = x.copy()
print y

# some new data (i.e., alpha_beta in the original code)
z = np.array([[10, 11, 12],
              [13, 14, 15]])
print z

# replace the iteration by numpy slicing to obtain the same result
x[0, :] = z[0, 0]
x[1, :] = z[1, 0]
print x

Upvotes: 1

Related Questions