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

#### 24 Answered Questions

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

**2008-10-31 05:55:36****Claudiu****2296240**View**2797**Score**24**Answer- Tags: python list data-structures append extend

#### 60 Answered Questions

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

**2009-12-25 04:28:23****Mottie****834407**View**877**Score**60**Answer- Tags: javascript arrays unique

#### 24 Answered Questions

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

**2008-09-25 21:01:57****Daryl Spitzer****590434**View**2804**Score**24**Answer- Tags: python oop methods python-decorators

#### 10 Answered Questions

### [SOLVED] Why is reading lines from stdin much slower in C++ than Python?

**2012-02-21 02:17:50****JJC****205841**View**1525**Score**10**Answer- Tags: python c++ benchmarking iostream getline

#### 49 Answered Questions

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

**2008-09-02 07:44:30****Carl Meyer****1271797**View**3399**Score**49**Answer- Tags: python dictionary merge

#### 57 Answered Questions

#### 21 Answered Questions

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

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

#### 21 Answered Questions

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

**2010-09-10 12:12:25****Ivo Flipse****71883**View**759**Score**21**Answer- Tags: python image-processing

## 4 comments

## @M.r_L 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