To do matrix multiplication, follow these steps:
- Check the dimensions:
The number of columns in the first matrix must equal the number of rows in the second matrix. If the first matrix is of size m×nm\times nm×n and the second matrix is n×pn\times pn×p, the resulting matrix will be of size m×pm\times pm×p
- Multiply rows by columns:
Each element in the resulting matrix is found by taking the dot product of a row from the first matrix and a column from the second matrix. This means you multiply corresponding elements and then sum those products. For example, to get the element in row iii, column jjj of the result:
resulti,j=∑k=1n(first matrixi,k×second matrixk,j)\text{result}{i,j}=\sum{k=1}^n(\text{first matrix}{i,k}\times \text{second matrix}{k,j})resulti,j=k=1∑n(first matrixi,k×second matrixk,j)
So, multiply each element of the iii-th row of the first matrix by the corresponding element of the jjj-th column of the second matrix, then add them all together
- Repeat for all elements:
Perform this dot product for every row of the first matrix and every column of the second matrix to fill the entire resulting matrix
Example
If matrix AAA is 1×31\times 31×3 and matrix BBB is 3×23\times 23×2, the result will be 1×21\times 21×2. To find the element in the first row, first column of the product, multiply the elements of the first row of AAA by the corresponding elements of the first column of BBB, then sum:
(3×4)+(1×3)+(4×5)=12+3+20=35(3\times 4)+(1\times 3)+(4\times 5)=12+3+20=35(3×4)+(1×3)+(4×5)=12+3+20=35
Repeat for other elements similarly
. In summary, matrix multiplication involves:
- Verifying compatible dimensions,
- Computing dot products of rows and columns,
- Building the resulting matrix from these sums