What Is Matrix in Data Structure With Example?
A matrix is a two-dimensional data structure that represents a collection of elements arranged in rows and columns. It is an essential concept in computer science and is widely used to solve various computational problems.
Structure of a Matrix
A matrix consists of rows and columns, forming a grid-like structure. Each element in the matrix is identified by its row and column index. The general representation of a matrix with ‘m’ rows and ‘n’ columns is as follows:
m x n Matrix:
- Rows are numbered from 1 to m.
- Columns are numbered from 1 to n.
- The element at row i and column j is denoted by A[i][j].
For example, consider the following 3×3 matrix:
3 x 3 Matrix:
- A[1][1] = 2, A[1][2] = 5, A[1][3] = -1
- A[2][1] = 0, A[2][2] = 9, A[2][3] = 4
- A[3][1] = -7, A[3][2] = 6, A[3][3] = 8
Operations on Matrices
In data structures, various operations can be performed on matrices. Some common operations include:
Addition of Matrices
To add two matrices, their corresponding elements are added together. The resulting matrix will have the same dimensions as the input matrices.
For example, let’s consider two matrices:
Matrix A:
- A[1][1] = 2, A[1][2] = 5
- A[2][1] = -3, A[2][2] = 7
Matrix B:
- B[1][1] = 4, B[1][2] = -1
- B[2][1] = 0, B[2][2] = 9
The addition of Matrix A and Matrix B will result in:
Matrix A + B:
- C[1][1] = A[1][1] + B[1][1] = 6
- C[1][2] = A[1][2] + B[1][2] = 4
- C[2][1] = A[2][1] + B[2][1] = -3
- C[2][2] = A[2][2] + B[2][2] = 16
Multiplication of Matrices
In matrix multiplication, the dot product of rows from the first matrix and columns from the second matrix is calculated to obtain the resulting matrix. The number of columns in the first matrix must be equal to the number of rows in the second matrix.
For example, consider the following matrices:
Matrix A:
- A[1][1] = 2, A[1][2] = -1
- A[2][1] = 3, A[2][2] = 4
Matrix B:
- B[1][1] = -3, B[1][2] = 5
- B[2][1] = 2, B[2][2] = -6
The multiplication of Matrix A and Matrix B will result in:
Matrix A * B:
- C[1][1] = (A[1][1]*B[1][1]) + (A[1][2]*B[2][1]) = -7
- C[1][2] = (A[1][1]*B[1][2]) + (A[1][2]*B[2][2]) = -17
- C[2][1] = (A[2][1]*B[1][1]) + (A[2][2]*B[2][1]) = -6
- C[2]][C]= (A[[22]]*B[[12]]) + (A[[21]]*B[[22]]) = -8
Transpose of a Matrix
The transpose of a matrix is obtained by interchanging its rows with columns. The resulting matrix will have the dimensions reversed compared to the original matrix.
For example, consider the following matrix:
Matrix A:
- A[1][1] = 2, A[1][2] = -4
- A[2][1] = 5, A[2][2] = 3
The transpose of Matrix A will be:
Transpose of Matrix A:
- A'[1][1] = A[1][1] = 2
- A'[1][2] = A[2][1] = 5
- A'[2][1] = A[1][2] = -4
- A'[2]][C]= A[[22]] =3
Conclusion
In summary, a matrix is a fundamental data structure in computer science that represents a collection of elements arranged in rows and columns. It allows for efficient manipulation of data and is used in various algorithms and mathematical operations. Understanding matrices and their operations is crucial for solving computational problems efficiently.