top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to multiply two matrices with different size using python?

+1 vote
721 views

I'm trying to multiply two matrices that has different size.

import numpy as np

a = np.random.randn(4, 3)
b = np.random.randn(4, 1)

print a
print b

How should I multiply a and b so that the multipled matrix's size is 4*3?

I want to multiply matrix 'b' to each row of matrix 'a'.
So that if matrix a is
[[1, 2, 3],
[2, 3, 4]]
and b is
[[2],
[3]]
a*b is
[[2, 4, 6],
[4, 6 ,8]]

Plz help me, if possible, plz use numpy

posted Nov 19, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You don't need to do anything special.

> import numpy as np
> a = np.array([[1,2,3],[2,3,4]])
> b = np.array([[2], [3]])
> a
array([[1, 2, 3],
 [2, 3, 4]])
> b
array([[2],
 [3]])
> a * b
array([[ 2, 4, 6],
 [ 6, 9, 12]])

I'm assuming this is actually the output you want.

answer Nov 19, 2016 by Deepti Singh
Similar Questions
+1 vote

I have a matrix of numbers representing the nodal points as follows:

Element No. Nodes

1 1 2 3 4
2 5 6 7 8
3 2 3 9 10
...........................
...........................
x 9 10 11 12
...........................

so this is a matrix of numbers 4 x n
Elements 1 and 3 are neighbours (as they share nodes 2 3). Similarly elements 3 and x are neighbours (they share nodes 9 and 10). I want to sort the matrix in such a way all the elements are sequentially arranged.

0 votes

suppose

A(n,m) = 
1 2 3         
4 5 6                            
7 8 9

and 

B(p, q) = 
1 1
1 1

What is best method to find min of square of difference of sub-matrices of A and B e.g.

sub-matrices of A =

1 2    |     2 3   |    4 5    |   5 6
3 4    |     5 6   |    7 8    |   8 9

Difference of first sub-matrix of A with B =

(1-1)  (2-1)    = |     0 1
(3-1)  (4-1)      |     2 3

sum of square of elements = 0*0 + 1*1 + 2*2 + 3*3 = 14

similar steps for other sub-matrices of A

Please suggest looking for an alternate method or algorithm which has time complexity less than O(n*m*p*q)

...