Say I have two arrays X=[A,B,C] and Y=[D,E,F], where each element is a 3 by 3 matrix. I would like to make an array Z=[AD,BE,CF] without using for loop. What should I do?
I have tried using np.tensordot(X,Y,axis=1) but it returns 9 products [[AD,AE,AF],[BD,BE,BF],[CD,CE,CF]]. the troublesome thing is that the matrix size for each element must be the same as the array length, say for 3 by 3 matrix, X and Y should have 3 elements each.
CodePudding user response:
You can use tensorflow.transpose
>>> a = tf.constant([1, 2, 3])
>>> b = tf.constant([4, 5, 6])
>>> tf.transpose([a, b])
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 4],
[2, 5],
[3, 6]], dtype=int32)>
or you can use zip
a = ("John", "Charles", "Mike")
b = ("Jenny", "Christy", "Monica")
x = zip(a, b)
(('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica'))
CodePudding user response:
It turns out the answer is incredibly simple. I just used np.matmul (X,Y) to achieve the result I wanted.