C语言中的矩阵乘法

C语言中的矩阵乘法,c,math,matrix,C,Math,Matrix,我试图用问题(2x2)中给出的C矩阵大小来解决一个矩阵乘法问题 我写了这段代码,但它并没有像我预期的那样打印结果。我想我遗漏了一点关于C的规则 我在这个代码中犯了什么错误 #include <stdio.h> int main() { int matA[2][2]={0,1,2,3}; int matB[2][2]={0,1,2,3}; int matC[2][2]; int i, j, k; for (i = 0; i < 2; i++

我试图用问题(2x2)中给出的C矩阵大小来解决一个矩阵乘法问题 我写了这段代码,但它并没有像我预期的那样打印结果。我想我遗漏了一点关于C的规则

我在这个代码中犯了什么错误

#include <stdio.h>
int main() {
    int matA[2][2]={0,1,2,3};
    int matB[2][2]={0,1,2,3};
    int matC[2][2];
    int i, j, k;
    for (i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            for(k = 0; k < 2; k++) {
                matC[i][j] += matA[i][k] * matB[k][j];
            }
            printf("%d\n",matC[i][j]);
        } 
    }
}

必须首先将
C
的元素初始化为零。

matC最初包含一些垃圾值。将martix初始化为全零。这可能会解决您的问题

您应该将
matC
初始化为全零。

以下是我使用的矩阵乘法代码:

for(i=0;i<M;i++){
    for(j=0;j<K;j++){
        matC[i][j]=0;
        for(k=0;k<N;k++){
            matC[i][j]+=matA[i][k]*matB[k][j];
        }
    }
}

对于(i=0;i问题在于

matC[i][j] += matA[i][k] * matB[k][j];
您正在向matC添加内容,但是当您创建它时,您没有初始化它,因此它有垃圾

你应该这样做:


int-matC[2][2]={0}
如果大小和依赖关系无关紧要,那么它将用0初始化所有矩阵。 有关功能,请参见此处:

它包含用于数学计算的优化例程,通过一些编译器优化,速度相当快


已经成功地将其用于3D开发中的矩阵运算。

用户可以通过以下方式获得任意给定大小的矩阵乘法:

#include<stdio.h>
void main()
{
    int r1, c1, r2, c2;

    printf("Enter number of rows and columns for matrix A : ");
    scanf("%d %d",&r1,&c1);

    printf("Enter number of rows and columns for matrix B : ");
    scanf("%d %d",&r2,&c2);

    int a[r1][c1], b[r2][c2], ab[r1][c2], ba[r2][c1],i,j,k,temp;

    if(c1==r2 && r1==c2)
    {
        printf("\nEnter element in matrix A : ");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
            {
                printf("\n Enter element : ");
                scanf("%d",&a[i][j]);
            }
        }
        printf("\nEnter element in B : ");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
            {
                printf("\nEnter element : ");
                scanf("%d",&b[i][j]);
            }
        }
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                temp=0;
                for(k=0;k<r2;k++)
                {
                    temp+=a[i][k]*b[j][k];
                }
                ab[i][j]=temp;
            }
        }
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c1;j++)
            {
                temp=0;
                for(k=0;k<r1;k++)
                {
                    temp+=b[i][k]*a[k][j];
                }
                ba[i][j]=temp;
            }
        }
        printf("\nMatrix A : ");
        for(i=0;i<r1;i++)
        {
            printf("\n\t");
            for(j=0;j<c1;j++)
            {
                printf("%d",a[i][j]);
            }
            printf("\n");
        }
        printf("\nMatrix B : ");
        for(i=0;i<r2;i++)
        {
            printf("\n\t");
            for(j=0;j<c2;j++)
            {
                printf("%d",b[i][j]);
            }
        }
        printf("\nMatrix multiplication of A*B : ");
        for(i=0;i<r1;i++)
        {
            printf("\n\t");
            for(j=0;j<c2;j++)
            {
                printf("\t%d",ab[i][j]);
            }
            printf("\n");
        }
        printf("\nMatrix multiplication of B*A : ");
        for(i=0;i<r2;i++)
        {
            printf("\n\t");
            for(j=0;j<c1;j++)
            {
                printf("\t%d",ba[i][j]);
            }
            printf("\n");
        }
    }
    else
        printf("\nMatrix Multiplication is not possible...!!!");
}
#包括
void main()
{
int r1,c1,r2,c2;
printf(“输入矩阵A的行数和列数:”);
scanf(“%d%d”、&r1和&c1);
printf(“输入矩阵B的行数和列数:”);
scanf(“%d%d”、&r2和&c2);
int a[r1][c1],b[r2][c2],ab[r1][c2],ba[r2][c1],i,j,k,temp;
如果(c1==r2&&r1==c2)
{
printf(“\n矩阵A中的输入元素:”);

对于(i=0;i您可能希望为结果矩阵动态分配内存。如果是这样,请使用
calloc()
分配并清除元素。
printMatrix()
被调用 打印结果,但此处未定义

/* matrix1: [rows1 x cols1];  matrix2: [rows2 x cols2]; product is
matrix3: [rows1 x cols2] if (cols1 == rows2) is true.  calloc to 
allocate / clear memory for matrix3.  Algorithm is O(n^3) */

float ** matrix3;
if (cols1 == rows2) {  // product matrix can be calculated
    // calloc product matrix3
    matrix3 = (float **)calloc(rows1, sizeof(float *));
    for (int i = 0; i < rows1; i++)
        matrix3[i] = (float *)calloc(cols2, sizeof(float));

    int i, j, k;
    float tmp;
    for (i = 0; i < rows1; i++) {
        for (j = 0; j < cols2; j++) {
            tmp = 0.0;
            for (k = 0; k < rows2; k++)
                tmp += matrix1[i][k] * matrix2[k][j];
            matrix3[i][j] = tmp;
        }
    }
    printMatrix(matrix3, rows1, cols2, 3);
    free(matrix3);
} else {   // cols1 != rows2
    puts("dimensional mismatch; can't multiply matrices");
}
/*matrix1:[rows1 x cols1];matrix2:[rows2 x cols2];产品为
matrix3:[rows1 x cols2]如果(cols1==rows2)为true,则调用
为matrix3分配/清除内存。算法为O(n^3)*/
浮动**x;
如果(cols1==rows2){//可以计算乘积矩阵
//calloc产品矩阵3
matrix3=(float**)calloc(行1,sizeof(float*);
对于(int i=0;i
谢谢大家。但为什么三个值正确,然后只有一个错误?(始终相同的值错误)谢谢大家。但是为什么三个值正确,然后只有一个错误?(始终相同的值错误)它取决于实现,可以有任何值。它具有您所说的值的事实应该是因为您正在使用的编译器。M、N和K是什么?@eri0o通常,它的:MxN大小矩阵和NxK。M是矩阵a的行,K是矩阵B的列,N是“计数”在你必须添加的这些部分中,N部分也被称为点积。我已经有一段时间没有看到这个了,所以请检查一下。
/* matrix1: [rows1 x cols1];  matrix2: [rows2 x cols2]; product is
matrix3: [rows1 x cols2] if (cols1 == rows2) is true.  calloc to 
allocate / clear memory for matrix3.  Algorithm is O(n^3) */

float ** matrix3;
if (cols1 == rows2) {  // product matrix can be calculated
    // calloc product matrix3
    matrix3 = (float **)calloc(rows1, sizeof(float *));
    for (int i = 0; i < rows1; i++)
        matrix3[i] = (float *)calloc(cols2, sizeof(float));

    int i, j, k;
    float tmp;
    for (i = 0; i < rows1; i++) {
        for (j = 0; j < cols2; j++) {
            tmp = 0.0;
            for (k = 0; k < rows2; k++)
                tmp += matrix1[i][k] * matrix2[k][j];
            matrix3[i][j] = tmp;
        }
    }
    printMatrix(matrix3, rows1, cols2, 3);
    free(matrix3);
} else {   // cols1 != rows2
    puts("dimensional mismatch; can't multiply matrices");
}