0
votes

I have written a python program that takes in numbers from a text file and generates two matrices. The size of these matrices are very large.

For example, matrix 1 is 5 * X and matrix 2 is X * 5 where X is a random number between 0 and 160

I have tried the following to multiply the matrices and used smaller numbers to verify the multiplication:

result = np.dot(matrix1,matrix2)
result = matrix1.dot(matrix2)
result = np.multiply(matrix1, matrix2[:, None])

These three ways do work when the dimensions of both matrixes are equal to each other. So, a 5 * 5 matrix multiplied by a 5 * 5 matrix will work. My code throws and error when it tries to multiply matrixes who's dimensions are not equal to each other. For example a 5 * 4 matrix multiplied by a 3 * 5 matrix will throw an error that always points to one of the three methods I have shown above

How am I able to multiply two matrixes of different dimensions?

1
matrix multiplication, by definition, requires that dimensions of the pair must be m x n followed by n x k. 5 x 4 followed by 3 x 5 does not meet that condition.Quang Hoang

1 Answers

1
votes

Matrix multiplication is defined for a matrix A that is MxN and B who is NxP thus resulting in a matrix AB who's dimensions are MxP.

You must have the same number of 'columns' in the first matrix as 'rows' in the second. The other two dimensions do not need to match.

You can achieve this by switching the order of matrices or transposing them, but those MxN and NxP two 'N' dimensions have to match.