5

我正在尝试用 C 解决矩阵乘法问题。问题中给出的矩阵大小 (2x2) 我写了这段代码,但它没有按我的预期打印结果。我想我错过了关于 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]);
        } 
    }
}

打印结果:

2 
3 
4195350
11
4

9 回答 9

9

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

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];
        }
    }
}

重要的是将答案矩阵设置为零(正如其他人所说的没有代码)。

于 2012-04-15T13:39:36.340 回答
5

问题是在行

matC[i][j] += matA[i][k] * matB[k][j];

你正在向 matC 添加东西,但是当你创建它时,你没有初始化它,所以它有垃圾。

你应该做这样的事情:

int matC[2][2] = {0}这将用 0 初始化所有矩阵

于 2012-04-15T13:45:42.123 回答
4

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

于 2012-04-15T13:36:09.567 回答
2

您可以通过以下方式由用户进行任何给定大小的矩阵乘法:

#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...!!!");
}
于 2017-01-10T15:41:51.473 回答
1

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

于 2012-04-15T13:35:08.093 回答
1

如果大小和依赖关系无关紧要,我建议使用 GNU Scientific Library。请参阅此处了解功能:http ://en.wikipedia.org/wiki/GNU_Scientific_Library

它包含用于数学计算的优化例程,并且通过一些编译器优化非常快。

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

于 2012-04-15T13:48:16.897 回答
0

您应该初始化matC为全零。

于 2012-04-15T13:38:21.590 回答
0

您可能希望为结果矩阵动态分配内存。如果是这样,请用于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");
}
于 2017-12-22T00:02:44.463 回答
0
//
//  main.c
//  Matrix Multiplicatiion.
//
//  Created by Devansh on 15/07/21.
// THIS WORKS FOR ANY MATRIX multiplication 
//

#include <stdio.h>

int main()
{
    int arow,acolumn,brow, bcolumn,i,j;
    int MAX=100;
    int a[MAX][MAX];
    int b[MAX][MAX];
    printf("enter rows of matrix a: ");
    scanf("%d", &arow);
    printf("enter columns of matrix a: ");
    scanf("%d", &acolumn);
    printf("enter rows of matrix b: ");
    scanf("%d", &brow);
    printf("enter columns of matrix b: ");
    scanf("%d", &bcolumn);
    if(brow != acolumn){
        printf("sorry we cannot multiply matrix a and b");
    } else {
        printf("enter the elements of matrix a:\n");
        for (i=0;i<arow;i++) {
            for(j=0;j<acolumn;j++) {
                scanf("%d", &a[i][j]);
            }
        }
        
        printf("enter the elements of matrix b:\n");
        for (i=0;i<brow;i++) {
            for (j=0;j<bcolumn;j++) {
                scanf("%d", &b[i][j]);
            }
        }
        int product[MAX][MAX];
        int sum=0;
        for (i=0;i<arow;i++) {
            for(j=0;j<bcolumn;j++){
                for(int k=0;k<brow;k++){
                    sum += a[i][k] * b[k][j];
                }
                product[i][j]= sum;
            }
        }
        for (i=0;i<arow;i++) {
            for (j=0;j<bcolumn;j++) {
                printf("%d   ", product[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
于 2021-07-19T05:50:35.770 回答