Transpose of the Matrix
Swap the rows and columns of the original matrix, that is, the data on [I][j] and [j][i] positions.
650) this.width=650; "title=" Picture 1.png "src=" http://s5.51cto.com/wyfs02/M01/81/FF/ Wkiom1dg3jdyhtjnaabwv61ugxk712.png "alt=" Wkiom1dg3jdyhtjnaabwv61ugxk712.png "/>
Program code:
#include <vector> //sparse matrix push pop operator[] and sequential table consistent template<class t>struct triple //defines a ternary group can be accessed directly as defined as struct{size_t _row;size_t _col; T _value; triple (Size_t row, size_t col, const t& value): _row (Row) , _col (COL), _value (value) {}}; template<class t>class sparsematrix{public : Sparsematrix (Size_t m, size_t n, const t&invalid): _M (M), _N (N), Invalid (invalid) { }sparsematrix (const t* a, size_t m, size_t n,const t & invalid)//const t& invalid indicates which is invalid data: _m (M), _n (N), invalid (invalid) {for (size_t i = 0; i < m; ++i) {for (size_t j = 0; &NBSP;J&NBSP;<&NBSP;N;&NBSP;++J) {if (a[i*n + j] != invalid) //not equal to invalid value {Triple <t> t (I, j,&NBSP;A[I*N&NBSP;+&NBSP;J]); _a.push_back (t); }}}} void display () {Size_t index = 0;for (size_t i = 0; i < _m; ++i) {for (size_t &NBSP;J&NBSP;=&NBSP;0;&NBSP;J&NBSP;<&NBSP;_N;&NBSP;++J) {if (index<_a.size () &&i == _a[index]._row && j == _a[index]._col) {Cout << _a[index]. value << " "; ++index;} else{cout << _invalid << " ";}} Cout << endl;} Cout << endl;} sparsematrix<t> transport () //transpose { //time Complexity o (number of valid data *n (column)) SPARSEMATRIX<T>&NBSP;SM (_n,_m,_invalid); for (size_t i = 0; i < n; ++i) {size_t index = 0;while (index < _a.size ()) {if (_a[index].col == i) {TRIPLE<T>&NBSp;t (_a[index]._col, _a[index]._row, _a[index]._value); Sm._a.push_back (t);} ++index;}} RETURN&NBSP;SM;} protected: //Storage Ternary Array//triple<t>* _a; Direct use of dynamic sequential table vector<triple<t>> _a;size_t _m;size_t _n; t _invalid;}; void test2 () {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 } }; SPARSEMATRIX<INT>&NBSP;SM ((int*) a,6,5,0) //cast to a one-dimensional array array 6 row 5 column illegal value 0SM. Display (); sparsematrix<int> sm2 = sm. Transport (); sm2. Display ();} #include <iostream>using namespace std; #include <stdlib.h> #include "Matrix.h" int main () {//test1 (); Test2 (); System ("pause"); return 0;}
Operation Result:
1 0 0 2 0 0
0 0 0 0 0 0
3 0 0 4 0 0
0 0 0 0 0 0
5 0 0 6 0 0
Quick Transpose
650) this.width=650; "title=" Picture 2.png "src=" http://s5.51cto.com/wyfs02/M02/81/FE/ Wkiol1dg3bmabqwjaaerbebcsiy780.png "alt=" Wkiol1dg3bmabqwjaaerbebcsiy780.png "/>
Program code:
#include <vector> //sparse matrix push pop operator[] and sequential table consistent template<class t>struct triple //defines a ternary group can be accessed directly as defined as struct{size_t _row;size_t _col; T _value; triple (Size_t row=0, size_t col=0, const t& value=t ())// Const temporary object has constancy: _row (Row), _col (col), _value (value) {}}; template<class t>class sparsematrix{public:sparsematrix (size_t m, size_t n, const t&invalid): _M (M) , _n (N), invalid (invalid) { }sparsematrix (const t* a, size_t m, size_t n,const t& invalid)//const t& invalid indicates which is invalid data: _m (M), _n (N), Invalid (invalid) {for (size_t i = 0; i < m; ++i) {for (size_t &NBSP;J&NBSP;=&NBSP;0;&NBSP;J&NBSP;<&NBSP;N;&NBSP;++J) {if (a[i*n + j] != invalid) //not equal to invalid value {triple<T> t (I, j, a[i*n + j]); _a.push_back (t); }}}} void Display () {size_t index = 0;for (size_t i = 0; i < _m; ++i) {for (size_t j = 0; j < _n; ++j) {if (index<_ A.size () &&i == _a[index]._row && j == _a[index]._col) {cout << _a[index].value << " "; ++index;} else{cout << _invalid << " ";}} Cout << endl;} Cout << endl;} sparsematrix<t> fasttransport () //fast Transpose {//Time complexity o ( Number of valid data +n) SPARSEMATRIX<T>&NBSP;SM (_n, _m, _invalid); int* rowcounts = new int[_n];//statistics Number of data memset (rowcounts, 0, sizeof (int) *_n); size_t index = 0; while (Index < _a.size ()) {Rowcounts[_A[index].col]++;++index;} int rowStart = new int[_N];rowStart[0] = 0;for (size_t i = 1; i < _n; ++i) {rowstart[i] = rowstart[i - 1] + ROWCOUNTS[I&NBSP;-&NBSP;1];} index = 0;sm._a.resize (_a.size ()) '; ' //cannot use pushbackwhile (Index < _a.size ()) //_ A.size number of valid data {size_t row = _a[index]._col;int& start = rowstart[row]; Triple<t> t (_a[index]._col, _a[index]._row, _a[index]._value); sm._a[start] = t;++start; // rowstart[row] [value ++++index;} delete[] rowcounts;delete[] &NBSP;ROWSTART;&NBSP;RETURN&NBSP;SM;} protected: //Storage Ternary Array//triple<t>* _a; Direct use of dynamic sequential table vector<triple<t>> _a;size_t _m;size_t _n;t _invalid;}; void test2 () {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 } }; SPARSEMATRIX<INT>&NBSP;SM ((int*) a,6,5,0) //cast to a one-dimensional array array 6 row 5 column illegal value 0SM. Display (); sparsematrix<int> sM2 = sm. Transport (); sm2. Display (); sparsematrix<int> sm3 = sm. Fasttransport (); sm3. Display ();} #include <iostream>using namespace std; #include <stdlib.h> #include "Matrix.h" int main () {//test1 (); Test2 (); System ("pause"); return 0;}
Operation Result:
1 0 0 2 0 0
0 0 0 0 0 0
3 0 0 4 0 0
0 0 0 0 0 0
5 0 0 6 0 0
This article is from the "10910765" blog, please be sure to keep this source http://10920765.blog.51cto.com/10910765/1783600
Transpose and quick transpose of C + + matrices