Matrix Basics
A Matrix is a two-dimensional array. A matrix consists of rows, columns and elements. Below is a matrix consisting of 3 rows, 4 columns of integer data values:
1 4 5 7
2 6 1 8
5 8 9 1
As you can see the above matrix has 3 (rows) X 4 (columns) = 12 elements. The data type of the all data elements in a particular matrix should be same. In above matrix, all the elements are integers, but we can declare other matrices with data type being Float, byte, char, String or Object etc. based on need.
Use cases for Matrices
| Storing, accessing, manipulating and filtering tables of data. |
| Matrices are used in dynamic programming and for solution of grid algorithms etc. |
| Matrices are also used for statistical analysis, game data processing and image processing. |
Advantages of Matrices
| Fast access to matrix elements using row and column indices. |
| Data can be easily visualized as a 2 D grid, this helps in intuitive storage of data related to graphic images, games like chess/ tic-tac-toe etc. |
Disadvantages of Matrices
| Matrix size is allocated at initialization and cannot be incremented or decremented. |
| Huge amount of contiguous memory is required to store large matrices. |
| If allocated array size is not fully utilized (that is only few indices are allocated, rest are unused), it can lead to wastage of memory. |
Time Complexity of Matrix operations:
Following table shows time complexity metrics for an Matrix with m*n elements
| Operation | Complexity |
|---|---|
| Accessing element when index is known | O(1) |
| Search element when index is unknown for a sorted matrix using binary search | O(log m*n) |
| Search element via linear traversal when index is unknown for unsorted array | O(m*n) |
| About Us | Privacy Policy | Contact us |