2018-08-10 12:04:32 8 Comments

I have two matrices A and B:

```
>>>A
[[1,1,1]
[1,1,1]]
>>>B
[2, 3]
```

I want to multiply them efficiently to get such result:

```
>>> A*B
[[2,2,2]
[3,3,3]]
```

I am looking for a more efficient solution than iterate in a loop. Can anyone help?

### Related Questions

#### Sponsored Content

#### 23 Answered Questions

### [SOLVED] What is the difference between @staticmethod and @classmethod?

**2008-09-25 21:01:57****Daryl Spitzer****656939**View**3059**Score**23**Answer- Tags: python oop methods python-decorators

#### 21 Answered Questions

### [SOLVED] Difference between __str__ and __repr__?

**2009-09-17 04:27:45****Casebash****544334**View**2353**Score**21**Answer- Tags: python magic-methods repr

#### 58 Answered Questions

#### 67 Answered Questions

### [SOLVED] Get all unique values in a JavaScript array (remove duplicates)

**2009-12-25 04:28:23****Mottie****1012210**View**1061**Score**67**Answer- Tags: javascript arrays unique

#### 27 Answered Questions

### [SOLVED] Difference between append vs. extend list methods in Python

**2008-10-31 05:55:36****Claudiu****2546671**View**3065**Score**27**Answer- Tags: python list data-structures append extend

#### 44 Answered Questions

### [SOLVED] How to merge two dictionaries in a single expression?

**2008-09-02 07:44:30****Carl Meyer****1471516**View**3851**Score**44**Answer- Tags: python dictionary merge

#### 34 Answered Questions

### [SOLVED] Most efficient method to groupby on a array of objects

**2013-01-21 20:18:24****Rail24****284253**View**318**Score**34**Answer- Tags: javascript arrays object group-by underscore.js

#### 22 Answered Questions

### [SOLVED] Peak detection in a 2D array

**2010-09-10 12:12:25****Ivo Flipse****78248**View**798**Score**22**Answer- Tags: python image-processing

## 4 comments

## @dimension 2018-08-10 12:30:57

Let :

Both have different shapes so we can't do matrix multiplication(element-wise) So we can do

`A.T`

which will convert the shape of the`A`

to`(3,2)`

## @Daniel F 2018-08-10 12:13:15

Simplest way is just

`(A*B.T).T`

But it's probably better to get used to broadcasting:This is functionally identical to @jpp's answer with

`np.multiply`

, but a little shorter to write## @jpp 2018-08-10 12:10:33

You can use

`np.multiply`

to multiply element-wise with broadcasting:## @Marek Justyna 2018-08-10 12:12:54

That excacly I am looking for! Element-wise multiplication :) Thank you!

## @Talha Irfan 2018-08-10 12:09:26

your question is defying the rules of mathematics. Matrix A and B cannot be multiplied together because the number of columns in A does not equal the number of rows in B. In this case, the multiplication of these two matrices is not defined

## @Sqoshu 2018-08-10 12:10:35

I think that by 'multiplication' he actually meant

`doing something to the matrices`

, but he didn't formulate it right so it's unknown what happens in certain situations.## @Talha Irfan 2018-08-10 12:37:41

well yes, I guess