您好,登錄后才能下訂單哦!
矩陣的轉(zhuǎn)置
將原矩陣的行、列對(duì)換,也就是將[i][j]和[j][i]位置上的數(shù)據(jù)對(duì)換。
程序代碼:
#include<vector> //稀疏矩陣push pop operator[] 和順序表一致 template<class T> struct Triple //定義一個(gè)三元組 可以直接訪問(wèn)的定義成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表示哪個(gè)是無(wú)效數(shù)據(jù) :_M(M) , _N(N) , invalid(invalid) { for (size_t i = 0; i < M; ++i) { for (size_t j = 0; j < N; ++j) { if (a[i*N + j] != invalid) //不等于無(wú)效值 { 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> Transport() //轉(zhuǎn)置 { //時(shí)間復(fù)雜度 O(有效數(shù)據(jù)的個(gè)數(shù)*N(列)) SparseMatrix<T> 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> t(_a[index]._col, _a[index]._row, _a[index]._value); sm._a.push_back(t); } ++index; } } return sm; } protected: //存三元組數(shù)組 //Triple<T>* _a; 直接用動(dòng)態(tài)順序表 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> sm((int*)a,6,5,0); //強(qiáng)制轉(zhuǎn)換成一維數(shù)組 數(shù)組 6行 5列 非法值0 sm.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; }
運(yùn)行結(jié)果:
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
快速轉(zhuǎn)置
程序代碼:
#include<vector> //稀疏矩陣push pop operator[] 和順序表一致 template<class T> struct Triple //定義一個(gè)三元組 可以直接訪問(wèn)的定義成struct { size_t _row; size_t _col; T _value; Triple(size_t row=0, size_t col=0, const T& value=T())//const臨時(shí)對(duì)象具有常性 :_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表示哪個(gè)是無(wú)效數(shù)據(jù) :_M(M) , _N(N) , invalid(invalid) { for (size_t i = 0; i < M; ++i) { for (size_t j = 0; j < N; ++j) { if (a[i*N + j] != invalid) //不等于無(wú)效值 { 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() //快速轉(zhuǎn)置 { //時(shí)間復(fù)雜度 O(有效數(shù)據(jù)個(gè)數(shù)+N) SparseMatrix<T> sm(_N, _M, _invalid); int* rowCounts = new int[_N];//統(tǒng)計(jì)轉(zhuǎn)置后數(shù)據(jù)個(gè)數(shù) 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 - 1]; } index = 0; sm._a.resize(_a.size())';' //不能用pushback while (index < _a.size()) //_a.size有效數(shù)據(jù)的個(gè)數(shù) { 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]里的值++ ++index; } delete[] rowCounts; delete[] rowStart; return sm; } protected: //存三元組數(shù)組 //Triple<T>* _a; 直接用動(dòng)態(tài)順序表 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> sm((int*)a,6,5,0); //強(qiáng)制轉(zhuǎn)換成一維數(shù)組 數(shù)組 6行 5列 非法值0 sm.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; }
運(yùn)行結(jié)果:
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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。