矩阵的转置
将原矩阵的行、列对换,也就是将[i][j]和[j][i]位置上的数据对换。
程序代码:
#include<vector> //稀疏矩阵push pop operator[] 和顺序表一致 template<class T> struct Triple //定义一个三元组 可以直接访问的定义成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表示哪个是无效数据 :_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) //不等于无效值 { 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() //转置 { //时间复杂度 O(有效数据的个数*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: //存三元组数组 //Triple<T>* _a; 直接用动态顺序表 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); //强制转换成一维数组 数组 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; }
运行结果:
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
快速转置
程序代码:
#include<vector> //稀疏矩阵push pop operator[] 和顺序表一致 template<class T> struct Triple //定义一个三元组 可以直接访问的定义成struct { size_t _row; size_t _col; T _value; Triple(size_t row=0, size_t col=0, const T& value=T())//const临时对象具有常性 :_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表示哪个是无效数据 :_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) //不等于无效值 { 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() //快速转置 { //时间复杂度 O(有效数据个数+N) SparseMatrix<T> sm(_N, _M, _invalid); int* rowCounts = new int[_N];//统计转置后数据个数 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有效数据的个数 { 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: //存三元组数组 //Triple<T>* _a; 直接用动态顺序表 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); //强制转换成一维数组 数组 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; }
运行结果:
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
本文出自 “10910765” 博客,请务必保留此出处http://10920765.blog.51cto.com/10910765/1783600
原文地址:http://10920765.blog.51cto.com/10910765/1783600