当前位置:网站首页>C语言使用随机数生成矩阵,实现三元组的快速转置。
C语言使用随机数生成矩阵,实现三元组的快速转置。
2020-11-10 10:51:00 【osc_b9r67jnt】
稀疏矩阵压缩及转置
1、随机数生成矩阵算法思想:使用time()函数给随机数播种,获得矩阵的总行数,总列数,非零元个数。总行数与总列数的值可直接存入三元组。每次生成一个非零元的行与列之后,由键盘输入非零元的大小e;并将其先存入二维数组a[][]。
2、压缩矩阵至三元组的算法思想:获得矩阵之后,将其压缩进三元组表当中,三元组的非零元个数初始中为0,因在生成随机矩阵的过程中可能会出现相同的行、列,导致矩阵非零元被覆盖,所以具体的非零元个数以矩阵当中的为准。遍历矩阵,如果存在非零元,则将该元素的行标i与列标j,存入三元组,同时非零元个数+1。
3、打印矩阵的算法思想:先打印每一行的元素,如果i,j与三元组表里的i,j相匹配,则打印元素,否则打印0。
4、三元组快速转置的算法思想:求得被转置矩阵M的每一列的非零元个数,进而求得每一列的第一个非零元在T.data中应有的位置,故需要两个辅助向量:num与copt。先使T.mu=M.nu、T.nu=M.mu、T.tu=M.tu。如果T.tu不等于0,则先求每一列中的非零元个数num[col],col从1到M.nu;再求位置copt[col],copt[col]=copt[col-1]+num[col-1] ,col从2到M.nu,copt[1]=1;最后进行转置操作,令M的i,j互换,每次互换一个元素则++copt[col]。
5、具体实现代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 200
#define OK 1
#define FALSE 0
#define OVERFLOW -2
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct
{
int i,j;
ElemType e;
}Triple;
typedef struct
{
Triple data[MAXSIZE+1];
int mu,nu,tu;
}TSMatrix;
Status CreateMatrix(TSMatrix *M)
{
int i,j,n,k; //行,列,非零元个数
int a[50][50]={
0}; //存放矩阵,初始值为0
ElemType e;
printf("本次稀疏矩阵的行数与列数均由随机数产生!\n");
srand((unsigned)time(NULL) + (unsigned)rand()); //以time()为种子生成随机数
(*M).mu=rand()%10+6; //随机行大小,取余是确定范围,mu在(6~15)之间
(*M).nu=rand()%10+7; //随机列大小
k=rand()%10+6; //随机非零元个数
printf("该稀疏矩阵的行数为%d,列数为%d,非零元个数为%d。",(*M).mu,(*M).nu,k);
(*M).data[0].i=0;
for(n = 1; n <= k; n++) //生成随机数矩阵
{
i=rand()%(*M).mu+1;
j=rand()%(*M).nu+1;
printf("请按行序输入第 %d 个非零元素,行%d,列%d,元素值:\n",n,i,j);
scanf("%d",&e);
a[i][j]=e;
}
(*M).tu=1;
for(i=1;i<=(*M).mu;i++) //以行为主序存入三元组
{
for(j=1;j<=(*M).nu;j++)
{
if(a[i][j]!=0)
{
(*M).data[M->tu].i = i; //行下标
(*M).data[M->tu].j = j; //列下标
(*M).data[M->tu].e = a[i][j]; //该下标所对应的值
M->tu++;
}
}
}
M->tu--;
return OK;
}
Status PrintMatrix(TSMatrix M)
{
int i,j,n=1;
printf("\n\n");
for(i=1;i<=M.mu;i++)
{
for(j=1;j<=M.nu;j++)
{
if(M.data[n].i==i&&M.data[n].j==j)
{
printf("%4d",M.data[n].e);
n++;
}
else
printf("%4d",0);
}
printf("\n");
}
}
Status FastTransposeSMatrix(TSMatrix M,TSMatrix *T)
{
int col,t,p,q;
int *num,*copt;
num=(int*)malloc((M.nu+1)*sizeof(int)); //给num分配内存空间;大小为M的列数+1,num[1]开始
copt=(int*)malloc((M.nu+1)*sizeof(int)); //copt表示该列的第一个非零元在T.data中的位置,copt大小与num相等
T->mu=M.nu; T->nu=M.mu; T->tu=M.tu; //T的行数等于M的列数,列数等于行数,非零元个数相等
if(T->tu) //非空
{
for(col=1;col<=M.nu;++col)
num[col]=0; //清零操作
for(t=1;t<=M.tu;++t)
++num[M.data[t].j]; //求M中每一列含非零元个数
copt[1]=1; //M的第一列的第一个非零元在T的第一个位置
for(col=2;col<=M.nu;++col)
copt[col]=copt[col-1]+num[col-1]; //累加操作,比较复杂,求每一列的第一个非零元在T.data中的位置
for(p=1;p<=M.tu;++p)
{
col=M.data[p].j; q=copt[col];
T->data[q].i=M.data[p].j; //转置操作
T->data[q].j=M.data[p].i;
T->data[q].e=M.data[p].e;
++copt[col]; //位置已用,计数加一
}
}
return OK;
}
void main()
{
TSMatrix M,T;
CreateMatrix(&M);
printf("原始矩阵如下:\n");
PrintMatrix(M); //打印M
FastTransposeSMatrix(M,&T);
printf("转置矩阵如下:\n");
PrintMatrix(T); //打印T
}
6、粉丝数上50,下次加上流程图,创作不易,谢谢支持。
版权声明
本文为[osc_b9r67jnt]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4258260/blog/4710724
边栏推荐
- 为什么要谨慎使用Arrays.asList、ArrayList的subList?
- 《Python Cookbook 3rd》笔记(2.4):字符串匹配和搜索
- 史上最全异常检测算法概述
- Leetcode 1-sum of two numbers
- What's the difference between delete, truncate, and drop, and what to do if you delete data by mistake
- csdn bug9:待加
- python math类
- GNU assembly language uses inline assembly to extend ASM
- Several solutions to the problem that selenium webdriver always fails to use click
- [paper reading notes] rosane, robust and scalable attributed network embedding for sparse networks
猜你喜欢

使用call、apply和bind解决js中烦人的this,事件绑定时的this和传参问题

About CentOS start error: the solution of failed to start crash recovery kernel arming

One of the 10 Greatest formulas in the world is well known

delete、truncate、drop 有什么区别,误删数据怎么办

史上最全异常检测算法概述

注册滴滴加不上车怎么办?要怎么处理?

CSDN bug7: to be added

csdn bug5:待加

CSDN bug9: to be added

CSDN bug11: to be added
随机推荐
【技术教程】Visual Studio 2017自建WebRTC中peerconnection_client程序报LNK2019 无法解析的外部符号错误
Filezilla server配置FTP服务器中的各种问题与解决方法
[elixir! #0073] beam 内置的内存数据库 —— ETS
Problems and solutions in configuring FTP server with FileZilla server
自定义注解!绝对是程序员装逼的利器!!
The high pass snapdragon 875 has won the title of Android processor, but the external 5g baseband has become its disadvantage
吴恩达《Machine Learning》精炼笔记 4:神经网络基础 - 知乎
mac终端Iterm2支持rz和sz的解决方案
想花钱速学互联网行业,大概花两三个月的时间,出来好找工作吗
如何更好地理解中间件和洋葱模型
刷题到底有什么用?你这么刷题还真没用
不用懂代码,会打字就可以建站?1111 元礼包帮你一站配齐!
nodejs 个人学习笔记(imook)pm2
[elixir! 0073] beam built-in memory database ETS
[technical course] peerconnection in webrtc self built by visual studio 2017_ The client program reported an external symbol error that LNK2019 could not resolve
getIServiceManager() 源码分析
网络安全工程师演示:原来***是这样控制你的服务器的!(下)
如何看待阿里云成立新零售事业部?
【高级测试工程师】新鲜出炉的三套价值18K的自动化测试面试(网易、字节跳动、美团)
商品管统——采购需求合并到采购单