Python program to Convert a Matrix to Sparse Matrix (original) (raw)
Last Updated : 15 Jan, 2025
Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.
Using a Dictionary
Converting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indices as keys and the corresponding values as dictionary values.
Python `
m = [ [1, 0, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4] ]
sparse_matrix = {} for i in range(len(m)): for j in range(len(m[i])): if m[i][j] != 0: sparse_matrix[(i, j)] = m[i][j]
print(sparse_matrix)
`
Output
{(0, 0): 1, (1, 2): 3, (2, 3): 4}
**Explanation:
- The program iterates through each element of the matrix and stores non-zero elements in a dictionary, where the key is a tuple of indices (i, j) and the value is the element.
- This reduces memory usage by only storing non-zero values.
Using a List of Tuples
Converting a matrix to a sparse matrix using a list of tuples involves storing the non-zero elements as tuples where each tuple contains the row index, column index and the value. This method is efficient for representing sparse matrices while maintaining the order of the elements.
Python `
m = [ [1, 0, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4] ]
sparse_matrix = [] for i in range(len(m)): for j in range(len(m[i])): if m[i][j] != 0: sparse_matrix.append((i, j, m[i][j]))
print(sparse_matrix)
`
Output
[(0, 0, 1), (1, 2, 3), (2, 3, 4)]
**Explanation:
- The program creates a list of tuples, each containing the row index, column index, and non-zero value of the matrix.
- This format allows easy tracking of positions and values for sparse matrices.
Similar Reads
- Python Program to Convert Matrix to String Program converts a 2D matrix (list of lists) into a single string, where all the matrix elements are arranged in row-major order. The elements are separated by spaces or any other delimiter, making it easy to represent matrix data as a string.Using List ComprehensionList comprehension provides a con 2 min read
- Python Program to Check if a given matrix is sparse or not A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem 4 min read
- Python - Convert Integer Matrix to String Matrix Given a matrix with integer values, convert each element to String. Input : test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6]] Output : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6']] Explanation : All elements of Matrix converted to Strings. Input : test_list = [[4, 5, 7], [10, 8, 3]] Output : [ 6 min read
- How to Create a Sparse Matrix in Python If most of the elements of the matrix have 0 value, then it is called a sparse matrix. The two major benefits of using sparse matrix instead of a simple matrix are: Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.Computing time: 3 min read
- Python Program to Construct n*m Matrix from List We are given a list we need to construct a n*m matrix from that list. For example, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we need to construct a 3*4 matrix so that resultant output becomes [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] .Using List ComprehensionThis method uses list comprehension 3 min read
- Python Program to find transpose of a matrix Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[][] is obtained by changing A[i][j] to A[j][i]. For Square Matrix: The below program finds transpose of A[][] and stores the result in B[][], we can change N for different dimension. Pyt 5 min read
- Python Program to Multiply Two Matrices Given two matrices, we will have to create a program to multiply two matrices in Python. Example: Python Matrix Multiplication of Two-DimensionPythonmatrix_a = [[1, 2], [3, 4]] matrix_b = [[5, 6], [7, 8]] result = [[0, 0], [0, 0]] for i in range(2): for j in range(2): result[i][j] = (matrix_a[i][0] 5 min read
- Python Program that filters out non-empty rows of a matrix Given Matrix, the following article shows how to filter all the Non-Empty rows of a matrix. In simpler terms, the codes provided below return a matrix after removing empty rows from it. Input : test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []] Output : [[4, 5, 6, 7], [9, 8, 1]] Explanation : All emp 4 min read
- Python - Convert Matrix to Dictionary The task of converting a matrix to a dictionary in Python involves transforming a 2D list or matrix into a dictionary, where each key represents a row number and the corresponding value is the row itself. For example, given a matrix li = [[5, 6, 7], [8, 3, 2], [8, 2, 1]], the goal is to convert it i 4 min read
- Convert Character Matrix to single String - Python In this article, we will explore various methods to convert character matrix to single string in Python. The simplest way to do is by using a loop.Using a LoopWe can use a loop (for loop) to iterate through each sublist and concatenate the elements of each sublist and then combine them into a final 2 min read