Determine if the matrices can be added (or subtracted). Two matrices can be added (or subtracted) if they have the same number of rows and columns. For example, if matrix A is:
1 2 3 4
4 3 2 1
and matrix B is:
5 6 7 8
8 7 6 5
then A and B each have four columns and two rows, so they can be added or subtracted.
Add the element in row 1, column 1 of the first matrix to the element in row 1, column 1 of the second matrix. In the example, if you are adding the matrices, this is 1 + 5 = 6. If you are subtracting B from A this is 1 - 5 = -4.
Repeat this for each element. In the example:
1 2 3 4 + 5 6 7 8 = 6 8 10 12
4 3 2 1 8 7 6 5 12 10 8 6
and
1 2 3 4 - 5 6 7 8 = -4 -4 -4 -4
4 3 2 1 8 7 6 5 -4 -4 -4 -4
Determine if the matrices can be multiplied. You can multiply A*B if the number of columns in matrix A is the same as the number of rows in matrix B. For example, if:
A = 1 2
3 4
5 6
and
B = 1 2 3
3 2 1
then matrix A has 3 rows and 2 columns and matrix B has 2 rows and 3 columns, and they can be multiplied. Note that the fact that A*B is possible does not mean that B*A is necessarily possible. The result will have as many rows as A and as many columns as B; in this example, it will be 3 rows and 3 columns.
Calculate element 1,1 of the product. Element p, q of the product are the sums of the products of the elements in the pth row of A and the qth column of B. So, for element 1, 1 this is 1*1 + 2*3 = 7.
Repeat this for each element. In the example, the product will be:
1*1 + 2*3 1*2 + 2*2 1*3 + 2*1
3*1 + 4*3 3*2 + 4*2 3*3 + 4*1
5*1 + 6*3 5*2 + 6*2 5*3 + 6*1
=
7 6 5
15 14 13
23 22 21