c++矩陣的轉置和快速轉置

矩陣的轉置

將原矩陣的行、列對換,也就是將[i][j]和[j][i]位置上的數據對換。

wKiom1dG3JDyhTJnAABwV61Ugxk712.png

程序代碼:

#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

 

快速轉置

wKioL1dG3bmAbQWjAAErBEBCSIY780.png 

程序代碼:

#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

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章