Skip to content
Home ยป Matrices and Sparse Matrices

Matrices and Sparse Matrices

โœ… Matrices and Sparse Matrices


โœ… 1. Matrix (Matrices)

โœ… Definition

A Matrix is a rectangular arrangement of elements (numbers/data) in the form of rows and columns.

โœ… It is written as:

[
A = [a_{ij}]
]

Where:

  • i = row number
  • j = column number

๐Ÿ“Œ Example of a Matrix (3ร—3):
[
A =
\begin{bmatrix}
1 & 2 & 3 \
4 & 5 & 6 \
7 & 8 & 9
\end{bmatrix}
]

โœ… In programming, matrices are represented using 2D Arrays.

Example:

int A[3][3];

โœ… Types of Matrices (Important)

โœ… 1. Row Matrix

Matrix having only one row.

Example (1ร—3):
[
[ 10\ \ 20\ \ 30 ]
]


โœ… 2. Column Matrix

Matrix having only one column.

Example (3ร—1):
[
\begin{bmatrix}
10\
20\
30
\end{bmatrix}
]


โœ… 3. Square Matrix

Matrix having same number of rows and columns.

Example (3ร—3)


โœ… 4. Diagonal Matrix

All elements except diagonal are 0.

Example:
[
\begin{bmatrix}
5 & 0 & 0\
0 & 3 & 0\
0 & 0 & 1
\end{bmatrix}
]


โœ… 5. Identity Matrix

Diagonal elements are 1, others are 0.

Example:
[
\begin{bmatrix}
1 & 0 & 0\
0 & 1 & 0\
0 & 0 & 1
\end{bmatrix}
]


โœ… 6. Zero (Null) Matrix

All elements are 0.

Example:
[
\begin{bmatrix}
0 & 0\
0 & 0
\end{bmatrix}
]


โœ… Applications of Matrices

Matrices are used in:
โœ… Computer graphics
โœ… Image processing
โœ… Scientific calculations
โœ… Engineering problems
โœ… Data representation in tables
โœ… Graphs (Adjacency matrix)



โœ… 2. Sparse Matrix

โœ… Definition

A Sparse Matrix is a matrix in which most of the elements are zero.

โœ… Condition:
If number of zero elements is greater than non-zero elements, then it is called a Sparse Matrix.

๐Ÿ“Œ Example:
[
\begin{bmatrix}
0 & 0 & 5 & 0\
0 & 0 & 0 & 0\
0 & 8 & 0 & 0\
0 & 0 & 0 & 0
\end{bmatrix}
]

Here:

  • Non-zero elements = 2 (5 and 8)
  • Remaining are zeros โ†’ many zeros โ†’ โœ… Sparse Matrix

โœ… Why Sparse Matrix is Used?

Storing sparse matrix in normal 2D array wastes memory because it stores too many zeros.

โœ… So we use special representation to save:

  • Memory (Space)
  • Processing time

โœ… 3. Sparse Matrix Representation

To store only non-zero elements, we use 3-tuple (Triplet) representation.

โœ… Triplet Form / 3-Tuple Representation

In this method, sparse matrix is stored using a table with 3 columns:

โœ… Columns are:

  1. Row number
  2. Column number
  3. Value

โœ… Example of Sparse Matrix

Matrix (4ร—4):
[
\begin{bmatrix}
0 & 0 & 3 & 0\
0 & 0 & 0 & 0\
0 & 5 & 0 & 0\
0 & 0 & 0 & 7
\end{bmatrix}
]

Non-zero values are:

  • 3 at (0,2)
  • 5 at (2,1)
  • 7 at (3,3)

โœ… Triplet Table:

RowColValue
443
023
215
337

๐Ÿ“Œ First row stores:

  • Total rows
  • Total columns
  • Total non-zero elements

โœ… 4. Advantages of Sparse Matrix

โœ… Benefits:

  • Saves memory space (stores only non-zero values)
  • Fast processing for big matrices
  • Efficient storage representation

โœ… 5. Disadvantages of Sparse Matrix

โŒ Limitations:

  • More complex than normal matrix
  • Accessing elements becomes slower because searching is needed
  • Not suitable if non-zero values are high

โœ… 6. Difference Between Matrix and Sparse Matrix

FeatureMatrixSparse Matrix
DefinitionRows and columns dataMostly zero values
StorageNormal 2D arraySpecial compact form
Memory UsageHigh for large matricesVery less
Best ForGeneral problemsLarge matrix with many zeros
ExampleMarks tableAdjacency matrix of sparse graph

โœ… Conclusion

A matrix is a 2D structure of elements arranged in rows and columns and is commonly stored using 2D arrays. A sparse matrix is a special matrix in which most elements are zero, and it is stored efficiently using triplet (row, column, value) representation to save memory and improve performance.