A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix.
TenSR: Multi-Dimensional Tensor Sparse Representation. Na Qi1, Yunhui Shi1, Xiaoyan Sun2, Baocai Yin13. 1Beijing Key Laboratory of Multimedia. Sparse Representations. A sparse representation is where most elements of the representation are near-zero with a few nonzero elements, which leads to a more semantic, efficient and robust.
Why to use Sparse Matrix instead of simple matrix ?
- Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.
- Computing time: Computing time can be saved by logically designing a data structure traversing only non-zero elements..
Example:
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value).
Sparse Matrix Representations can be done in many ways following are two common representations:
- Array representation
- Linked list representation
![Representation Representation](/uploads/1/2/4/7/124750111/357369985.png)
Method 1: Using Arrays
2D array is used to represent a sparse matrix in which there are three rows named as
- Row: Index of row, where non-zero element is located
- Column: Index of column, where non-zero element is located
- Value: Value of the non zero element located at index – (row,column)
C++
// using Array { int sparseMatrix[4][5] = {0 , 0 , 3 , 0 , 4 }, {0 , 0 , 0 , 0 , 0 }, }; int size = 0; for ( int j = 0; j < 5; j++) size++; // number of columns in compactMatrix (size) must be // sparseMatrix int k = 0; for ( int j = 0; j < 5; j++) { compactMatrix[1][k] = j; k++; { printf ( '%d ' , compactMatrix[i][j]); printf ( 'n' ); return 0; |
Java
// using Array { public static void main(String[] args) int sparseMatrix[][] { 0 , 0 , 3 , 0 , 4 }, { 0 , 0 , 0 , 0 , 0 }, }; int size = 0 ; { { { } } // number of columns in compactMatrix (size) must be // sparseMatrix int k = 0 ; { { { compactMatrix[ 1 ][k] = j; k++; } { { } } } /* This code contributed by PrinciRaj1992 */ |
Output:
Method 2: Using Linked Lists
In linked list, each node has four fields. These four fields are defined as:
- Row: Index of row, where non-zero element is located
- Column: Index of column, where non-zero element is located
- Value: Value of the non zero element located at index – (row,column)
- Next node: Address of the next node
// using Linked Lists #include<stdlib.h> // Node to represent sparse matrix { int row_position; struct Node *next; void create_new_node( struct Node** start, int non_zero_element, { temp = *start; { temp = ( struct Node *) malloc ( sizeof ( struct Node)); temp->row_position = row_index; temp->next = NULL; else while (temp->next != NULL) r = ( struct Node *) malloc ( sizeof ( struct Node)); r->row_position = row_index; r->next = NULL; } // This function prints contents of linked list void PrintList( struct Node* start) struct Node *temp, *r, *s; while (temp != NULL) temp = temp->next; printf ( 'n' ); printf ( 'column_postion: ' ); { r = r->next; printf ( 'n' ); while (s != NULL) printf ( '%d ' , s->value); } } int main() // Assume 4x5 sparse matrix { {0 , 0 , 5 , 7 , 0 }, {0 , 2 , 6 , 0 , 0 } struct Node* start = NULL; for ( int i = 0; i < 4; i++) if (sparseMatric[i][j] != 0) create_new_node(&start, sparseMatric[i][j], i, j); PrintList(start); return 0; |
![Representation Representation](http://katbailey.github.io/mf/images/tensorflow_learn_1.png)
Output:
As a Dictionary where row and column numbers are used as keys and values are matrix entries. This method saves space but sequential access of items is costly.
As a list of list. The idea is to make a list of rows and every item of list contains values. We can keep list items sorted by column numbers.
This article is contributed by Akash Gupta.If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
The conventional sparse model relies on data representation in the form of vectors. It represents the vector-valued or vectorized one dimensional (1D) version of an signal as a highly sparse linear combination of basis atoms from a large dictionary. The 1D modeling, though simple, ignores the inherent structure and breaks the local correlation inside multidimensional (MD) signals. It also dramatically increases the demand of memory as well as computational resources especially when dealing with high dimensional signals. In this paper, we propose a new sparse model TenSR based on tensor for MD data representation along with the corresponding MD sparse coding and MD dictionary learning algorithms. The proposed TenSR model is able to well approximate the structure in each mode inherent in MD signals with a series of adaptive separable structure dictionaries via dictionary learning. The proposed MD sparse coding algorithm by proximal method further reduces the computational cost significantly. Experimental results with real world MD signals, i.e. 3D Multi-spectral images, show the proposed TenSR greatly reduces both the computational and memory costs with competitive performance in comparison with the state-of-the-art sparse representation methods. We believe our proposed TenSR model is a promising way to empower the sparse representation especially for large scale high order signals.