稀疏矩阵的存储,及矩阵的转置
来源:互联网 发布:优米网倒闭知乎 编辑:程序博客网 时间:2024/05/19 07:09
include“Matrix”
#pragma once#include<iostream>#include<vector>using namespace std;template<class T>struct Triple{ T _value; size_t _row; size_t _col;};template<class T>class SparseMatrix{public: SparseMatrix(T matrix[6][5] = NULL , size_t Row = 0, size_t Col = 0, const T& invalue = T()) :_Row(Row) , _Col(Col) { for (int i = 0; i < Row; i++) { for (int j = 0; j < Col; j++) { if (matrix[i][j] != invalue) { Triple<T> t; t._row = i; t._col = j; t._value = matrix[i][j]; v.push_back(t); } } } } void Display() { int index = 0; for (int i = 0; i < _Row; i++) { for (int j = 0; j < _Col; j++) { if (index < v.size() && v[index]._row == i && v[index]._col == j) { cout << v[index]._value << " "; index++; } else { cout << "0" << " "; } } cout << endl; } cout << endl; } SparseMatrix<T> Trasport() { SparseMatrix<T> tmp; tmp._Row = _Col; tmp._Col = _Row; for (int i = 0; i < _Col; i++) { int index = 0; while (index < v.size()) { if (v[index]._col == i) { Triple<T> t; t._row = v[index]._col; t._col = v[index]._row; t._value = v[index]._value; tmp.v.push_back(t); } index++; } } return tmp; } //快速转置 SparseMatrix<T> FastTranspost() { SparseMatrix<T> tmp; tmp._Row = _Col; tmp._Col = _Row; int* RowCount = new T[_Col];// 统计转置后矩阵每一行的数据个数 int* RowStart = new T[_Col];// 统计转置后的矩阵每行在压缩矩阵中存储的开始位置 memset(RowCount, 0, sizeof(int)*_Col);//初始化RowCount数组 memset(RowStart, 0, sizeof(int)*_Col);//初始化RowStart数组 // 统计转置后矩阵每一行的数据个数 RowCounts = {2, 0, 2, 0, 2}; int index = 0; while (index < v.size()) { RowCount[v[index]._col]++; index++; } //统计转置后的矩阵每行在压缩矩阵中存储的开始位置 RowStart ={0, 2, 2, 4, 4}; RowStart[0] = 0; for (int i = 1; i < _Col;i++) { RowStart[i] = RowCount[i - 1] + RowStart[i - 1]; } // 建立转置以后的压缩三元组数组 index = 0; while (index < v.size()) { tmp.v.push_back(Triple<T>()); index++; } // 根据计算好每一行的起始位置进行转置 index = 0; while (index < v.size()) { size_t FtpIndex = RowStart[v[index]._col]++; tmp.v[FtpIndex]._row = v[index]._col; tmp.v[FtpIndex]._col = v[index]._row; tmp.v[FtpIndex]._value = v[index]._value; index++; } delete[] RowCount; delete[] RowStart; return tmp; }private: vector<Triple<T>> v; size_t _Row; size_t _Col;};
main.cpp
#include"Matrix.h"const int ROW = 6;const int COL = 5;void TestMatrix(){ int matrix[ROW][COL] = { { 1, 0, 3, 0, 5 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 1, 0, 3, 0, 5 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }; SparseMatrix<int> sm(matrix,ROW,COL,0); sm.Display(); SparseMatrix<int> smtp = sm.Trasport(); smtp.Display(); SparseMatrix<int> smftp = sm.FastTranspost(); smftp.Display();}int main(){ TestMatrix(); getchar(); return 0;}
0 0
- 稀疏矩阵的存储,及矩阵的转置
- 稀疏矩阵的压缩存储及转置
- 稀疏矩阵的压缩存储及转置算法
- 稀疏矩阵的压缩存储及转置
- 稀疏矩阵的压缩存储及快速转置
- 【矩阵】对称矩阵及稀疏矩阵的压缩存储
- 稀疏矩阵的存储及运算
- 稀疏矩阵存储及转置
- 矩阵(稀疏矩阵)的压缩存储
- 对称矩阵、稀疏矩阵的压缩存储
- 稀疏矩阵的顺序存储
- 稀疏矩阵的存储格式
- 稀疏矩阵的压缩存储
- 稀疏矩阵的压缩存储
- 稀疏矩阵的压缩存储
- 稀疏矩阵的压缩存储
- 稀疏矩阵的压缩存储
- 稀疏矩阵的压缩存储
- [Leetcode]Add Two Numbers
- BeanPropertyRowMapper小错误
- 二分查找算法
- Centos 6.4 python 2.6 升级到 2.7
- C#暴力屏蔽任务管理器
- 稀疏矩阵的存储,及矩阵的转置
- phpexcel
- setjmp和longjmp函数使用详解
- matlab字符函数
- 数据库索引与多表联查
- 【Android学习笔记】Android中的进程和线程
- Android 读取SD卡中的text文件内容
- 日历控件合集
- Vue.js使用props传递数据驼峰式与短横线隔