![]() |
VOOZH | about |
In R, matrices are two-dimensional arrays of data numeric, logical or complex organized in rows and columns. Matrices are used to depict the data in a structured and well-organized format. It is necessary to enclose the elements of a matrix in parentheses or brackets.
A matrix is defined by its order:
Order = number of rows × number of columns
Example:
[,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9
There are four basic operations i.e. DMAS (Division, Multiplication, Addition, Subtraction) that can be done with matrices. Both the matrices involved in the operation should have the same number of rows and columns.
Matrix addition is the process of adding corresponding elements from two matrices. This operation is only defined when the two matrices have the same dimensions (i.e., same number of rows and columns).
This loop adds each element of B and C and stores the result in sum_matrix.
[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 [,1] [,2] [,3] [1,] 8 12 16 [2,] 10 14 18
Explanation:
A and B are both 2x3 matrices.[,1] [,2] [,3] [1,] 1+0i 5.4+0i 4+0i [2,] 2+3i 3.0+0i 5+0i [,1] [,2] [,3] [1,] 2+0i 0.1+0i 4+0i [2,] 0+0i 3.0+0i 5+0i [,1] [,2] [,3] [1,] 3+0i 5.5+0i 8+0i [2,] 2+3i 6.0+0i 10+0i...
Explanation:
Properties of Matrix Addition:
Matrix subtraction follows the same structural rules as addition. It involves subtracting corresponding elements from the first matrix by the second matrix.
[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 [,1] [,2] [,3] [1,] -6 -6 -6 [2,] -6 -6 -6
Here in the above code, the elements of diff matrix are the subtraction of the corresponding elements of A and B through nested for loops.
Using '-' operator for matrix subtraction
[,1] [,2] [,3] [1,] 1+0i 5.4+0i 4+0i [2,] 2+3i 3.0+0i 5+0i [,1] [,2] [,3] [1,] 2+0i 0.1+0i 4+0i [2,] 0+0i 3.0+0i 5+0i [,1] [,2] [,3] [1,] -1+0i 5.3+0i 0+0i [2,] 2+3i 0.0+0i 0+0i...
Properties of Matrix Subtraction:
In element-wise multiplication, each element in one matrix is multiplied by the corresponding element in the second matrix. This is different from matrix product (dot product).
[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 [,1] [,2] [,3] [1,] 7 27 55 [2,] 16 40 72
The elements of sum are the multiplication of the corresponding elements of A and B through nested for loops.
Using '*' operator for matrix multiplication
[,1] [,2] [1,] 1 3 [2,] 2 4 [,1] [,2] [1,] 5 7 [2,] 6 8 [,1] [,2] [1,] 5 21 [2,] 12 32
Properties of Matrix Multiplication:
Element-wise division divides each element of one matrix by the corresponding element in another matrix.
[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 [,1] [,2] [,3] [1,] 0.1428571 0.3333333 0.4545455 [2,] 0.250...
The elements of div matrix are the division of the corresponding elements of B and C through nested for loops.
Using '/' operator for matrix division
[,1] [,2] [1,] 10 30 [2,] 20 40 [,1] [,2] [1,] 2 5 [2,] 4 8 [,1] [,2] [1,] 5 6 [2,] 5 5
Properties of Matrix Division: