Simple Operations on Matrices
Here are some simple operations you can implement on matrices:
1) Matrix Addition
Matrices A and B can be added, if they are the same size (e.g., m x n). The matrix sum C = A + B also will be a m x n matrix, where for each element cij = aij + bij and 1 ≤ i ≤ m and 1 ≤ j ≤ n.
To add these two matrices:
You would use this solution:
Matrix addition is commutative (e.g., A + B = B + A) and associative (e.g., C + (D + E) = (C + D) + E).
2) Matrix Subtraction
Two matrices (A and B) can be subtracted if they are the same size (e.g., k x l). The matrix difference D = A – B will also be a k x l matrix with the element dij = aij – bij, where 1 ≤ i ≤ k and 1 ≤ j ≤ l.
To subtract matrix B from matrix A:
You would use this solution:
Matrix subtraction is neither commutative nor associative.
3) Matrix Scalar Multiplication
If A is an m x n matrix and the constant k is a number, then the scalar product of k and A is a new matrix B = kA, where bij = kaij and 1 ≤ i ≤ m and 1 ≤ j ≤ n.
To find B = 5∙A, where:
You would use this solution:
Scalar multiplication is commutative and associative:
k(AB) = (kA)B = A(kB) = (AB)k