c++中稀疏矩阵的转置
Transpose of a sparse matrix in C++
我是一个c++新手。这里,我试着找出一个稀疏矩阵的转置。下面是代码:
#include<iostream>
using namespace std;
class transposeM{
int m1[20][20],m2[20][20],i,j,row,column,t;
public:
void read(){
t=0;
cout<<"Enter the number of row: n";
cin>>row;
cout<<"enter the number of column: n";
cin>>column;
for(i=0;i<row;i++){
for(j=0;j<column;j++){
cin>>m1[i][j];
if(m1[i][j]){
t++;
// cout<<"first t is:"<<t;
//if non zero
m2[t][0]=i+1;
m2[t][1]=j+1;
m2[t][2]=m1[i][j];
}
}
}
m2[0][0]=row;
m2[0][1]=column;
m2[0][2]=t;
}
void displaysp(){
cout<<"sparse matrix is: n";
for(i=0;i<=t;i++){
for(j=0;j<3;j++){
cout<<m2[i][j]<<" ";
}
cout<<"n";
}
}
void transpose(){
int transpose[20][3];
transpose[0][0]=m2[0][0];
transpose[0][1]=m2[0][1];
transpose[0][2]=m2[0][2];
cout<<"Transpose is: n";
int q=1;
for(i=1;i<=column;i++){
for(int p=1;p<=t;p++){
if(m2[p][1]==i){
transpose[q][0]=m2[p][0];
transpose[q][1]=m2[p][1];
transpose[q][2]=m2[p][2];
q++;
}
}
}
for(i=0;i<=column;i++){
for(j=0;j<3;j++){
cout<<transpose[i][j]<<" ";
}
cout<<"n";
}
}
void display(){
for(i=0;i<row;i++){
for(j=0;j<column;j++){
cout<<m1[i][j]<<" ";
}
cout<<"n";
}
}
};
int main(int argc,char ** argv){
transposeM obj;
obj.read();
obj.display();
obj.displaysp();
obj.transpose();
return 0;
}
输出:Enter the number of row:
2
enter the number of column:
2
0
1
2
0
0 1
2 0
sparse matrix is:
2 2 2
1 2 1
2 1 2
Transpose is:
2 2 2
2 1 2
1 2 1
但是出了问题;读取矩阵并将其转换为稀疏矩阵是可以的。
转置的简单代码片段为:(transpose
将是m2
的转置)
for(i=0;i<column;i++) {
for(j=0;j<row;j++) {
transpose[i][j] = m2[j][i];
}
}
你有不必要的复杂的东西
可以为您节省一些麻烦并帮助简化问题的是,当您转置矩阵时,您可以在i
和j
的情况下切换row
和column
索引。所以你可以写一个display()
方法,只设置一个转置标志或参数,例如:
void displaysp()
{
// m is the number of rows in matrix
// n is the number of columns in matrix
unsigned int tmpM=m, tmpN=n ;
if( transposeFlag )
{
tmpM = n ;
tmpN = m ;
}
for(unsigned i=0;i<tmpM;i++)
{
for(unsigned j=0;j<tmpN;j++)
{
if( transposeFlag )
{
cout<<m2[j][i]<<" ";
}
else
{
cout<<m2[i][j]<<" ";
}
}
cout<<"n";
}
}
问题已解决,我正在发布代码:
#include<iostream>
using namespace std;
class transposeM{
int m1[20][20],m2[20][20],i,j,row,column,t;
public:
void read(){
t=0;
cout<<"Enter the number of row: n";
cin>>row;
cout<<"enter the number of column: n";
cin>>column;
for(i=0;i<row;i++){
for(j=0;j<column;j++){
cin>>m1[i][j];
if(m1[i][j]){
t++;
m2[t][0]=i+1;
m2[t][1]=j+1;
m2[t][2]=m1[i][j];
}
}
}
m2[0][0]=row;
m2[0][1]=column;
m2[0][2]=t;
}
void displaysp(){
cout<<"sparse matrix is: n";
for(i=0;i<=t;i++){
for(j=0;j<3;j++){
cout<<m2[i][j]<<" ";
}
cout<<"n";
}
}
void transpose(){
int transpose[20][3];
transpose[0][0]=m2[0][1];
transpose[0][1]=m2[0][0];
transpose[0][2]=m2[0][2];
cout<<"Transpose is: n";
int q=1;
for(i=1;i<=column;i++){
for(int p=1;p<=t;p++){
if(m2[p][2]==i){
transpose[q][0]=m2[p][1];
transpose[q][1]=m2[p][0];
transpose[q][2]=m2[p][2];
q++;
}
}
}
for(i=0;i<=column;i++){
for(j=0;j<3;j++){
cout<<transpose[i][j]<<" ";
}
cout<<"n";
}
}
void display(){
for(i=0;i<row;i++){
for(j=0;j<column;j++){
cout<<m1[i][j]<<" ";
}
cout<<"n";
}
}
};
int main(int argc,char ** argv){
transposeM obj;
obj.read();
obj.display();
obj.displaysp();
obj.transpose();
return 0;
}
输出:Enter the number of row:
2
enter the number of column:
3
0
1
2
0
0
2
0 1 2
0 0 2
sparse matrix is:
2 3 3
1 2 1
1 3 2
2 3 2
Transpose is:
3 2 3
2 1 1
3 1 2
3 2 2
相关文章:
- 转置矩阵:交换元素不会更改值
- 使用 Eigen 3 库编写一个带有转置作为参数的函数
- 犰狳(C++)中的快速阵列置换(广义张量转置)
- 为什么转置这个 std::vector<std::vector<std::string> > 这么慢?
- 转置结构容器
- 我的转置矩阵代码有什么问题?
- 数组的转置和乘法
- 在C++中使用矢量转置 2D 矩阵
- 特征:块转置
- MKL矩形矩阵Inplace转置:不使用多个核心
- 并行转置不同的矩阵
- 关于次级对角线的转置(翻转)矩阵
- 输出是从您输入的矩阵中打印出矩阵的转置,但我的代码只是打印出您输入的第一个矩阵
- 如何在阵列火中避免翻转和转置的memcpy?
- CUDA矩阵与共享内存转置
- C 阵列的复合物共轭转置
- 转置期间的动态内存分配
- 转置的一维矢量的平均矢量
- 在CUDA中具有共享MEM的非方面矩阵转置
- 通过矩阵转置优化矩阵乘法