In the matrix, there is a kind of very important matrix, that is-----sparse matrix.
The so-called sparse matrix, that is, in the matrix, the number of valid data is far less than the number of invalid data (and these data are not ordered in order). Let's start with an example of a sparse matrix:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/F0/wKiom1cNoXqgYIYUAAAPLNUT7Mw525.png "title=" Pz{9wk _42lasvq1a[wq%]u1.png "alt=" Wkiom1cnoxqgyiyuaaaplnut7mw525.png "/>
The number of valid data is only 6, the rest is invalid data 0.
Then we will sparse matrix exists in the compression matrix, set up a ternary group, using {Row,col,value} to store each valid data, ternary group according to the original matrix position, in order to store the priority of the row.
We build a structure:
struct triple//defines a ternary group used to store the x, y, and coordinate values of the sparse matrix {int _row; int _col; T _value;};
Each valid data (ternary group) exists in the sequential table vector, and the printed data is printed according to the characteristics of the sequential table.
Transpose of the matrix:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/F0/wKiom1cNoyLgdIoCAAAda7pd8GI613.png "title=" 8w[ ncjxwn0q ([ebg1_9f17q.png "alt=" Wkiom1cnoylgdiocaaada7pd8gi613.png "/>
So, transpose is to swap the rows and columns of the original matrix, that is, the data on [I][j] and [j][i] positions.
The code is as follows:
#include <vector>template<class t>struct triple//defines a ternary group that stores the X, y, and coordinate values of a sparse matrix { int _row; int _col; T _value; triple (Int row, int col, int value) :_row (Row) , _col (col) , _value (value) {}};template<class T> Class sparsematrix{public: sparsematrix (t* a, int m, int N, const t& invalid) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (Invalid != a[i*n+j]) { // Each valid data (ternary group) exists in the order table vector triple <t> tmp (I, j, a[i*n + j]); _a.push_back (TMP), } } } } //printing sparse matrices in coordinate form Void display (int m, int n, const t& invalid) { cout << Print sparse matrix in coordinate form << endl; cout << "{" << "x," << " " << " y, " << " " << " value " < < "}" <<endl; for (int k = 0; k < _a.size (); k++) { cout << "{" << _a[k]._row << ", " << _a[k]._col << ", " << _a[k ]._value << " " << "}" << endl; } } //printing sparse matrices in matrix form void Displaymatrix (int m, int n, const t& invalid) { cout << "Printing sparse matrices in matrix form" << endl; int k = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (K<_a.size () &&_a[k]._row = =&NBSP;I&NBSP;&&&NBSP;_A[K]._COL&NBSP;==&NBSP;J) { cout << _a[k]._value << " "; k++; } else { cout << invalid << " "; } } cout << endl; } } //Matrix Transpose SparseMatrix<T> Transport (t* a, int m, int n, const t& invalid) { cout << "Matrix transpose:" << endl ; int b[5][6]; //Row Size for (int i = 0; i < m; i+ +) //Column interchange size { for (int j = 0; j < n; j++) { //convert elements of one-dimensional array to [j][i] form b[j][i] = a[i*n + j]; } } sparsematrix<t> tranmatrix ((int*) b, 5, 6, 0); return tranmatrix; }protected: vector <Triple <T>> _a; };void&nbsP Test () { int a[6][5] = { { 1, 0, 3, 0, 5 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 2, 0, 4, 0, 6 }, { 0 , 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, }; int m = 6; int n = 5; sparsematrix <INT>&NBSP;SM ((int*) a, m, n, 0); sm. Display ( m, n, 0); sm. Displaymatrix ( m, n, 0); sparsematrix<int> sm1 (int*) a, m, n, 0); sm1 = sm. Transport ((int*) a, m, n, 0); sm1. Display ( n, m, 0); sm1. Displaymatrix (n, m, 0);} Int main () { test (); system ("Pause"); return 0;}
This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1763284
"Data structure" sparse structure and sparse matrix compression storage, the matrix transpose