日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言矩阵存储,C语言实现特殊矩阵存储

發布時間:2024/9/30 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言矩阵存储,C语言实现特殊矩阵存储 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面實現的特殊矩陣存儲方法

三元組順序存儲方法&轉置

#include

#include

#define MAXSIZE 12500

// 三元組順序表示稀疏矩陣

// written at 2018-6-1 23:37:22

typedef int ElemType;

typedef struct{

int i; // row

int j; // column

ElemType e;

}tripple;

typedef struct{

tripple data[MAXSIZE+1]; // 0 not use

int mu, nu, tu; // 行,列,非零元總數

}tsMatrix;

tripple init_tribble(int i, int j, ElemType e){

tripple temp;

temp.i = i;

temp.j = j;

temp.e = e;

return temp;

}

void print_sparse_matrix(tsMatrix temp){

printf("**********START************\n");

printf("i\tj\tv\t\n");

for (int i=1; i<=temp.tu; i++){

printf("%d\t%d\t%d\t\n",temp.data[i].i,temp.data[i].j,temp.data[i].e);

}

printf("***********END*************\n");

}

void transposeMatrix(tsMatrix m, tsMatrix *t){

// 按列轉置矩陣

t->mu = m.nu; // step 1

t->nu = m.mu; // step 1

t->tu = m.tu;

// step 2 & step 3

if(t->tu){

int q=1;

for (int col=1;col<=m.nu;++col){

for (int p=1; p<=m.tu;++p){

if (m.data[p].j == 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;

q++;

}

}

}

}

}

void quickTranspose(tsMatrix m, tsMatrix *t){

// 通過預先確認M中每一列的第一個非零元在b.data中的應有的位置,則可以省時間

t->mu = m.nu;

t->nu = m.mu;

t->tu = m.tu;

if (t->tu){

int num[m.nu + 1]; // 0 not use

for (int col=1; col<=m.nu; col++) num[col] = 0; // 初始化num數組

for (int temp=1; temp<=m.tu; temp++) ++num[m.data[temp].j];

int cpot[m.nu + 1]; // 0 not use

cpot[1] = 1; // 第一列中第一個元素肯定是在第一個位置

// 求第col列中第一個非零元在b.data中的序號

for (int col=2; col<=m.nu;++col){

cpot[col]=cpot[col-1]+num[col-1];

}

for (int p=1;p<=m.tu;++p){

int col = m.data[p].j;

int q=cpot[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;

++cpot[col];

}

}

}

void main(){

// init the martix and let the matrix not empty

tsMatrix temp;

temp.data[1] = init_tribble(1, 2, 12);

temp.data[2] = init_tribble(1, 3, 9);

temp.data[3] = init_tribble(3, 1, -3);

temp.data[4] = init_tribble(3, 6, 14);

temp.data[5] = init_tribble(4, 3, 24);

temp.data[6] = init_tribble(5, 2, 18);

temp.data[7] = init_tribble(6, 1, 15);

temp.data[8] = init_tribble(6, 4, -7);

// 課本97頁中的M矩陣

temp.mu = 6;

temp.nu = 7;

temp.tu = 8;

print_sparse_matrix(temp);

tsMatrix t;

tsMatrix t2;

transposeMatrix(temp, &t2);

quickTranspose(temp, &t);

printf("\t按列轉置\t\n");

print_sparse_matrix(t2);

printf("\t快速轉置\t\n");

print_sparse_matrix(t);

}

輸出結果如下:

**********START************

i j v

1 2 12

1 3 9

3 1 -3

3 6 14

4 3 24

5 2 18

6 1 15

6 4 -7

***********END*************

按列轉置

**********START************

i j v

1 3 -3

1 6 15

2 1 12

2 5 18

3 1 9

3 4 24

4 6 -7

6 3 14

***********END*************

快速轉置

**********START************

i j v

1 3 -3

1 6 15

2 1 12

2 5 18

3 1 9

3 4 24

4 6 -7

6 3 14

***********END*************

行邏輯鏈接的順序表

#include

#include

#define MAXSIZE 12500

/*

行邏輯連接的順序表

為了便于隨機存取任意一行的非零元引入的第二種表示方法

written at 2018-6-2 18:26:30

*/

typedef int ElemType;

typedef struct{

int i; // row

int j; // column

ElemType e;

}tripple;

typedef struct RLSMatrix{

tripple data[MAXSIZE+1];

int mu, nu, tu;

int rops[MAXRC + 1]; // 各行第一個非零元的位置表

}RLSMatrix;

總結

以上是生活随笔為你收集整理的c语言矩阵存储,C语言实现特殊矩阵存储的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。