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

#### 62 Answered Questions

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

**2009-12-25 04:28:23****Mottie****769142**View**798**Score**62**Answer- Tags: javascript arrays unique

#### 58 Answered Questions

#### 10 Answered Questions

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

**2012-02-21 02:17:50****JJC****194040**View**1471**Score**10**Answer- Tags: c++ python benchmarking readline getline

#### 48 Answered Questions

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

**2008-09-02 07:44:30****Carl Meyer****1199847**View**3248**Score**48**Answer- Tags: python python-3.x dictionary merge

#### 23 Answered Questions

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

**2008-10-31 05:55:36****Claudiu****2205705**View**2710**Score**23**Answer- Tags: python list data-structures append extend

#### 25 Answered Questions

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

**2008-09-25 21:01:57****Daryl Spitzer****563945**View**2689**Score**25**Answer- Tags: python oop methods python-decorators

#### 21 Answered Questions

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

**2009-09-17 04:27:45****Casebash****469662**View**2117**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****69274**View**739**Score**21**Answer- Tags: python image-processing

## 4 comments

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