2

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, i.e.,[[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]]])
2
  • What’s your question, exactly? Please include all code and data in the post itself, not as images. Commented Nov 30, 2019 at 3:46
  • thanks for quick reply, i posted a question with fig but it seems doesn't work, and i have updated the question. Commented Nov 30, 2019 at 3:59

2 Answers 2

1
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.

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

2 Comments

yes, but the output is not what i want, the output what i want is like[[[1,2][2,-2],[3,-3]][[40,-40],[50,-50],[60,-60]]]
c = np.array([[[1]],[[10]]]) should do the job
0

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)

1 Comment

the output what i want is like[[[1,2][2,-2],[3,-3]][[40,-40],[50,-50],[60,-60]]], which means c[0] times first group 0f a, and c[1] times second group

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.