c - 考虑稀疏矩阵获取转置矩阵(可能与排序有关)

标签 c math matrix

我想获得稀疏矩阵并打印它,但效果不太好。

#include <stdio.h>
#include <stdlib.h>

#define MAX_TERMS 10

typedef struct {
    int row;
    int col;
    int value;
} element;

typedef struct SparseMatrix {
    int rows;
    int cols;
    int terms;
    element data[MAX_TERMS];
} SparseMatrix;

void printSM(SparseMatrix temp);
SparseMatrix transpose(SparseMatrix a);

void main(){
    SparseMatrix A = {3,3,3, {{0, 1, 3},
                              {1, 0, 8},
                              {2, 2, 5}}};
    SparseMatrix B;
    B = transpose(A);
    printSM(A);
    printSM(B);
    getchar();
}

void printSM(SparseMatrix temp) {
    printf(">>Sparse Matrix<< \n");
    int r, c, i = 0;
    for (r = 0; r < temp.rows; r++) {
        for (c = 0; c < temp.cols; c++) {
            if (r == temp.data[i].row && c == temp.data[i].col) {
                printf("%3d", temp.data[i].value);
                i++;
            }
            else
            {
                printf("%3d", 0);
            }
        }
        printf("\n");
    }
}

是给定的问题 我编写了一些代码来处理它

SparseMatrix transpose(SparseMatrix a){
    SparseMatrix b=a;
    for (int index = 0; index < a.terms; index++) {
        if (b.data[index].col != b.data[index].row) {
            b.data[index].col = a.data[index].row;
            b.data[index].row = a.data[index].col;
        }
    }
    return b;

看起来还可以。 我认为它应该运作良好,但这是一个错误。

答案一定是 0 8 0 3 0 0 0 0 5 但我只得到了 0 0 0 3 0 0 0 0 0

我使用 for 循环通过转置函数进行了测试,我知道我完全做得很好 我有 b.data[0,1,2]={{1, 0, 3},{0, 1, 8},{2, 2, 5}} b.col=b.low=b.terms=3

我查看了我的打印功能,我发现顺序应该很重要。 例如,当我转置这些并得到这些时 (3,2, 4)>>>>>>>>(2,3, 4) (3,1, 4)>>>>>>>>(1,3, 4) printSM 函数可能会忽略第二个,因为指示 row 的变量已经增加了 2 并且可以忽略 cols=1 时的情况 我知道这个 printSM 功能不值得推荐,我想将其更改为更好的功能,但我无法触及此功能,因为这是给定的问题......

所以我尝试将选择排序插入到我的转置函数中,以便

for (int index = 0; index < b.terms - 1; index++) {
    int tmp=index;
    for (int indexp = index + 1; indexp < b.terms; indexp++) {
        if (b.data[tmp].col > b.data[indexp].col) {
            tmp = indexp;
        }
    }
            int ctmp = b.data[tmp].col;
            int rtmp = b.data[tmp].row;
            int vtmp = b.data[tmp].value;
            b.data[tmp].col = b.data[index].col;
            b.data[tmp].row = b.data[index].row;
            b.data[tmp].value = b.data[index].value;
            b.data[index].col = ctmp;
            b.data[index].row = rtmp;
            b.data[index].value = vtmp;
}

并且......我得到了相同的答案 0 0 0 3 0 0 0 0 0

噢不.... 我可以得到你的帮助吗?

最佳答案

稀疏矩阵仅使用大量内存需求的一小部分。

然而,您将整个矩阵传入或传出您的函数。如果您确实要使用大型稀疏矩阵,则会耗尽堆栈空间。

您应该更改函数签名以使用指针并使用 malloc 仅分配所需的内存。

(这可能不是您问题的答案,但向您指出这个概念错误很重要)

关于c - 考虑稀疏矩阵获取转置矩阵(可能与排序有关),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41298830/

相关文章:

python - 使用 Python 进行高效、大规模的竞赛评分

c - 在 C 中搜索另一个数组时将值插入数组?

c - Unicode代码指向utf8和wctomb

javascript - KeysPressed 是位掩码的。如何取消位掩码并将其放入关键状态的结构中? (Javascript)

c++ - 如何反转矩阵

r - 将不是 Square 加权邻接矩阵转换为 R 中的 igraph 对象

c - 程序无法在 Ubuntu 12.04 和 Windows XP 上运行

c++ - 如何执行从 char* 缓冲区加载到内存中的代码?

java - 不确定如何实现以下算法

python - 使用 LU 分解求逆矩阵