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

#### 8 Answered Questions

#### 44 Answered Questions

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

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

#### 63 Answered Questions

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

**2009-12-25 04:28:23****Mottie****919137**View**956**Score**63**Answer- Tags: javascript arrays unique

#### 57 Answered Questions

#### 22 Answered Questions

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

**2008-09-25 21:01:57****Daryl Spitzer****622394**View**2912**Score**22**Answer- Tags: python oop methods python-decorators

#### 21 Answered Questions

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

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

#### 24 Answered Questions

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

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

#### 10 Answered Questions

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

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

#### 21 Answered Questions

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

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

## 4 comments

## @DataBender 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