稀疏矩阵三元组的快速转置
生活随笔
收集整理的這篇文章主要介紹了
稀疏矩阵三元组的快速转置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
#include <stdio.h>
using namespace std;#define MAXSIZE 1024
#define ElemType int
#define Status int// 三元組
typedef struct Triple{int i, j; // i jElemType e; // 值
}Triple;// 由三元組組成的表
typedef struct TSMatrix{Triple data[MAXSIZE + 1]; // 在表中對應位置的三元組值int nu, mu, tu; // nu:行數,mu:列數,tu:元素總數
}TSMatrix;// 快速轉置矩陣
void FastTransposeMatrix(TSMatrix& M, TSMatrix& N)
{// 1.首先行列對換N.nu = M.mu;N.mu = M.nu;N.tu = M.tu;if (N.tu){// num:下標表示元素在表中哪一個列,對應的值表示該列有幾個元素(比如 第一列有二個元素:num[1] = 2)// copt:下標表示列數,對應的值為該列第一個元素在轉置后的表中對應的位置(比如 第二列第一個元素在位置3:copt[2] = 3)int num[100] = {0};int copt[100] = {0};// 遍歷M,得到每一列有幾個元素for (int i = 1; i <= M.tu; ++i){++num[M.data[i].j];}// col:對應元素所在的列值;int col;// 第一列第一個元素明顯為1 所以置copt[1]為1copt[1] = 1;// 從第二列開始,找到每列第一個元素所對應的位置for (col = 2; col <= M.mu; ++col){copt[col] = copt[col - 1] + num[col - 1];}// pos 暫時存放元素轉置后對應的位置int pos;// 遍歷Mfor (int tmp = 1; tmp <= M.tu; ++tmp){// 得到元素對應的列值col = M.data[tmp].j;// 找到該元素在新表中的位置pos = copt[col];// 轉置N.data[pos].i = M.data[tmp].j;N.data[pos].j = M.data[tmp].i;N.data[pos].e = M.data[tmp].e;// 該列在copt中記錄+1++copt[col];}}return;}int main()
{TSMatrix M; // 原三元表TSMatrix N; // 快速轉置后的三元表cout << "please input M Matrix:" << endl;cout << "please input how many row and how many col : ";cin >> M.nu >> M.mu;cout << "input how many Elem : ";cin >> M.tu;cout << "input the i and j and value : ";for (int i = 1; i <= M.tu; ++i){cin >> M.data[i].i >> M.data[i].j >> M.data[i].e;}FastTransposeMatrix(M, N);cout << "after Fast Transpose the Matrix's row is " << M.nu << ", col is " << M.mu << ", amount is " << M.tu << endl;cout << "the value is " << endl;for (int j = 1; j <= N.tu; ++j){printf("%d %d %d\n", N.data[j].i, N.data[j].j, N.data[j].e);}return 0;
}
總結
以上是生活随笔為你收集整理的稀疏矩阵三元组的快速转置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给语音信号加混响的常用方法(方法二)
- 下一篇: 光纤激光器原理与特性详解