矩阵直接转置和快速转置的小例子,VC++6.0通过
2010-12-17 07:37
399 查看
#include <stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
typedef struct
{
int i;
int j;
int e;
}Triple;
typedef struct
{
int mu;
int nu;
int tu;
Triple data[MAXSIZE+1];
}Tabletype;
void out_matrix(Tabletype m);
void Transpose1(Tabletype a,Tabletype *b);
void Transpose2(Tabletype a,Tabletype *b);
void main()
{
Tabletype a={6,7,8,{{1,2,12,},{1,3,9},{3,1,-3},{3,6,14},{4,3,24},{5,2,18},{6,1,15},{6,4,-7}}};
Tabletype b;
printf("The original matrix is :/n");
out_matrix(a);
Transpose1(a,&b);
printf("The followed matrix is the Transpose1 of the front matrixe/n");
out_matrix(b);
Transpose2(b,&a);
printf("The followed matrix is the Transpose2 of the front matrixe/n");
out_matrix(a);
}
//输出矩阵函数
void out_matrix(Tabletype m)
{
int i,j,k;
k=0;
for (i=1;i<=m.mu;i++)
{
for (j=1;j<=m.nu;j++)
if((m.data[k].i==i)&&(m.data[k].j==j))
{
printf("%5d",m.data[k].e);
k++;
}
else
printf("%5d",0);
printf("/n");
}
}
//直接转置
void Transpose1(Tabletype a,Tabletype *b)
{
int p,q,col;
(*b).mu=a.nu;
(*b).nu=a.mu;
(*b).tu=a.tu;
if((*b).tu)
{
q=0;
for(col=1;col<=a.nu;col++)
for(p=0;p<a.tu;p++)
if(a.data[p].j==col)
{
(*b).data[q].i=a.data[p].j;
(*b).data[q].j=a.data[p].i;
(*b).data[q].e=a.data[p].e;
q++;
}
}
}
//快速转置
void Transpose2(Tabletype a,Tabletype *b)
{
int p,q,col,t;
int num[MAXSIZE+1];
int cpot[MAXSIZE+1];
(*b).mu=a.nu;
(*b).nu=a.mu;
(*b).tu=a.tu;
if((*b).tu)
{
for(col=1;col<=a.nu;col++)
num[col]=0;
for(t=0;t<a.tu;t++)
num[a.data[t].j]++;
cpot[1]=1;
for(col=2;col<=a.nu;col++)
cpot[col]=cpot[col-1]+num[col-1];
for(p=0;p<a.tu;p++)
{
col= a.data[p].j;
q=cpot[col]-1;
(*b).data[q].i=a.data[p].j;
(*b).data[q].j=a.data[p].i;
(*b).data[q].e=a.data[p].e;
cpot[col]++;
}
}
}
#include<stdlib.h>
#define MAXSIZE 20
typedef struct
{
int i;
int j;
int e;
}Triple;
typedef struct
{
int mu;
int nu;
int tu;
Triple data[MAXSIZE+1];
}Tabletype;
void out_matrix(Tabletype m);
void Transpose1(Tabletype a,Tabletype *b);
void Transpose2(Tabletype a,Tabletype *b);
void main()
{
Tabletype a={6,7,8,{{1,2,12,},{1,3,9},{3,1,-3},{3,6,14},{4,3,24},{5,2,18},{6,1,15},{6,4,-7}}};
Tabletype b;
printf("The original matrix is :/n");
out_matrix(a);
Transpose1(a,&b);
printf("The followed matrix is the Transpose1 of the front matrixe/n");
out_matrix(b);
Transpose2(b,&a);
printf("The followed matrix is the Transpose2 of the front matrixe/n");
out_matrix(a);
}
//输出矩阵函数
void out_matrix(Tabletype m)
{
int i,j,k;
k=0;
for (i=1;i<=m.mu;i++)
{
for (j=1;j<=m.nu;j++)
if((m.data[k].i==i)&&(m.data[k].j==j))
{
printf("%5d",m.data[k].e);
k++;
}
else
printf("%5d",0);
printf("/n");
}
}
//直接转置
void Transpose1(Tabletype a,Tabletype *b)
{
int p,q,col;
(*b).mu=a.nu;
(*b).nu=a.mu;
(*b).tu=a.tu;
if((*b).tu)
{
q=0;
for(col=1;col<=a.nu;col++)
for(p=0;p<a.tu;p++)
if(a.data[p].j==col)
{
(*b).data[q].i=a.data[p].j;
(*b).data[q].j=a.data[p].i;
(*b).data[q].e=a.data[p].e;
q++;
}
}
}
//快速转置
void Transpose2(Tabletype a,Tabletype *b)
{
int p,q,col,t;
int num[MAXSIZE+1];
int cpot[MAXSIZE+1];
(*b).mu=a.nu;
(*b).nu=a.mu;
(*b).tu=a.tu;
if((*b).tu)
{
for(col=1;col<=a.nu;col++)
num[col]=0;
for(t=0;t<a.tu;t++)
num[a.data[t].j]++;
cpot[1]=1;
for(col=2;col<=a.nu;col++)
cpot[col]=cpot[col-1]+num[col-1];
for(p=0;p<a.tu;p++)
{
col= a.data[p].j;
q=cpot[col]-1;
(*b).data[q].i=a.data[p].j;
(*b).data[q].j=a.data[p].i;
(*b).data[q].e=a.data[p].e;
cpot[col]++;
}
}
}
相关文章推荐
- [转]矩阵求导---通过一个例子快速上手
- 通过一个例子快速上手矩阵求导
- 通过矩阵求两个向量集中元素两两之间的欧氏距离(python实现),比直接分开求更快速
- 【LB】稀疏矩阵的快速转置原理及其算法
- 稀疏矩阵的列序递增法和一次定位快速转置法
- 稀疏矩阵的相关操作(存储、转置、快速转置、相加)
- 矩阵的快速转置
- 稀疏矩阵的压缩存储及转置,快速转置法,C++代码实现
- 稀疏矩阵的压缩存储以及快速转置
- 通过Excel自带的查询分析器快速完成从SQL Server中导出数据的例子(通用类)
- 数据结构 矩阵的快速转置 矩阵相乘(行逻辑连接顺序表)
- C++实现稀疏矩阵的压缩存储、转置、快速转置
- 稀疏矩阵的存储与快速转置
- 【数据结构之旅】稀疏矩阵的快速转置
- 【数据结构】稀疏结构及稀疏矩阵的压缩存储,矩阵的(快速)转置
- 广义表的应用--稀疏矩阵的快速转置(三元组)
- 稀疏矩阵快速转置算法分析
- 稀疏矩阵的转置和快速转置
- 稀疏矩阵的压缩存储与快速转置 三元组法 类与对象实现
- 稀疏矩阵的普通转置与快速转置算法