简体   繁体   中英

python numpy 3-d matrix times 1-d array

I have a multi-dimentional array named a (dimention is (2,3,3)) and another array named c (dimention is (2,)) as following code: how to get the output as the combination--->(a[0]*c[0],a[1]*c[1]) without loops, which means 1 times first group of a, ie,[[1,2],[2,-2],[3,-3]] and 10 times second group of a, namely [[4,-4],[5,-5],[6,-6]]. Btw, i have tried a*c, np.multipy(a,c), etc, but it seems like 1 times first column of a and 10 times second column, that is not what i want. Many thanks.

In [88]: a = np.array([[[1,2],[2,-2],[3,-3]],[[4,-4],[5,-5],[6,-6]]])                                                                

In [89]: a                                                                                                                           
Out[89]: 
array([[[ 1,  2],
        [ 2, -2],
        [ 3, -3]],

       [[ 4, -4],
        [ 5, -5],
        [ 6, -6]]])
In [90]: c = np.array([1,10])                                                                                                        

In [91]: c                                                                                                                           
Out[91]: array([ 1, 10])

In [92]: a*c                                                                                                                         
Out[92]: 
array([[[  1,  20],
        [  2, -20],
        [  3, -30]],

       [[  4, -40],
        [  5, -50],
        [  6, -60]]])

The output that i want is like

array([[[ 1,  2],
        [ 2, -2],
        [ 3, -3]],

       [[ 40, -40],
        [ 50, -50],
        [ 60, -60]]])
import numpy as np

a = np.array([[[1,2],
      [2,-2],
      [3,-3]],
     [[4,-4],
      [5,-5],
      [6,-6]]])

c = np.array([1,10])

print(a*c)

Output:

[[[  1  20]
  [  2 -20]
  [  3 -30]]

 [[  4 -40]
  [  5 -50]
  [  6 -60]]]

I'm guessing that's what you asked.

What is your question? How to multiply? That you could do like this:

import numpy as np

a = np.array([[[1,2],[2,-2],[3,-3]], [[4,-4],[5,-5],[6,-6]]]);
c = np.array([1, 10]);

print a.dot(c)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM