使用数组的矩阵乘法生成错误结果

时间:2018-10-23 06:41:21

标签: c arrays matrix-multiplication

以下代码将两个不同阶的矩阵相乘并返回结果,即 mat1(6x3)* mat2(3x6),并以 dest

返回6x6矩阵

问题是结果与计算值不匹配。另外,如果您观察到结果 mat2 正在更改其内容。

  

PS:迭代次数:3,0,1 mat1 [3] [1] = 0.000000 mat2 [1] [0] = 20.000000   dest [3] [0] = 0.000000

     

mat2 [1] [0]的原始内容为零。

输出:

iterals: 0, 0, 0    mat1[0][0] = 10.000000  mat2[0][0] = 1.000000   dest[0][0] = 10.000000
iterals: 0, 0, 1    mat1[0][1] = 0.000000   mat2[1][0] = 0.000000   dest[0][0] = 10.000000
iterals: 0, 0, 2    mat1[0][2] = 0.000000   mat2[2][0] = 0.000000   dest[0][0] = 10.000000
iterals: 0, 1, 0    mat1[0][0] = 10.000000  mat2[0][1] = 0.000000   dest[0][1] = 0.000000
iterals: 0, 1, 1    mat1[0][1] = 0.000000   mat2[1][1] = 0.000000   dest[0][1] = 0.000000
iterals: 0, 1, 2    mat1[0][2] = 0.000000   mat2[2][1] = 0.000000   dest[0][1] = 0.000000
iterals: 0, 2, 0    mat1[0][0] = 10.000000  mat2[0][2] = 0.000000   dest[0][2] = 0.000000
iterals: 0, 2, 1    mat1[0][1] = 0.000000   mat2[1][2] = 1.000000   dest[0][2] = 0.000000
iterals: 0, 2, 2    mat1[0][2] = 0.000000   mat2[2][2] = 0.000000   dest[0][2] = 0.000000
iterals: 0, 3, 0    mat1[0][0] = 10.000000  mat2[0][3] = 0.000000   dest[0][3] = 0.000000
iterals: 0, 3, 1    mat1[0][1] = 0.000000   mat2[1][3] = 0.000000   dest[0][3] = 0.000000
iterals: 0, 3, 2    mat1[0][2] = 0.000000   mat2[2][3] = 0.000000   dest[0][3] = 0.000000
iterals: 0, 4, 0    mat1[0][0] = 10.000000  mat2[0][4] = 0.000000   dest[0][4] = 0.000000
iterals: 0, 4, 1    mat1[0][1] = 0.000000   mat2[1][4] = 0.000000   dest[0][4] = 0.000000
iterals: 0, 4, 2    mat1[0][2] = 0.000000   mat2[2][4] = 1.000000   dest[0][4] = 0.000000
iterals: 0, 5, 0    mat1[0][0] = 10.000000  mat2[0][5] = 0.000000   dest[0][5] = 0.000000
iterals: 0, 5, 1    mat1[0][1] = 0.000000   mat2[1][5] = 0.000000   dest[0][5] = 0.000000
iterals: 0, 5, 2    mat1[0][2] = 0.000000   mat2[2][5] = 0.000000   dest[0][5] = 0.000000
iterals: 1, 0, 0    mat1[1][0] = 0.000000   mat2[0][0] = 1.000000   dest[1][0] = 0.000000
iterals: 1, 0, 1    mat1[1][1] = 0.000000   mat2[1][0] = 0.000000   dest[1][0] = 0.000000
iterals: 1, 0, 2    mat1[1][2] = 0.000000   mat2[2][0] = 0.000000   dest[1][0] = 0.000000
iterals: 1, 1, 0    mat1[1][0] = 0.000000   mat2[0][1] = 0.000000   dest[1][1] = 0.000000
iterals: 1, 1, 1    mat1[1][1] = 0.000000   mat2[1][1] = 0.000000   dest[1][1] = 0.000000
iterals: 1, 1, 2    mat1[1][2] = 0.000000   mat2[2][1] = 0.000000   dest[1][1] = 0.000000
iterals: 1, 2, 0    mat1[1][0] = 0.000000   mat2[0][2] = 0.000000   dest[1][2] = 0.000000
iterals: 1, 2, 1    mat1[1][1] = 0.000000   mat2[1][2] = 1.000000   dest[1][2] = 0.000000
iterals: 1, 2, 2    mat1[1][2] = 0.000000   mat2[2][2] = 0.000000   dest[1][2] = 0.000000
iterals: 1, 3, 0    mat1[1][0] = 0.000000   mat2[0][3] = 0.000000   dest[1][3] = 0.000000
iterals: 1, 3, 1    mat1[1][1] = 0.000000   mat2[1][3] = 0.000000   dest[1][3] = 0.000000
iterals: 1, 3, 2    mat1[1][2] = 0.000000   mat2[2][3] = 0.000000   dest[1][3] = 0.000000
iterals: 1, 4, 0    mat1[1][0] = 0.000000   mat2[0][4] = 0.000000   dest[1][4] = 0.000000
iterals: 1, 4, 1    mat1[1][1] = 0.000000   mat2[1][4] = 0.000000   dest[1][4] = 0.000000
iterals: 1, 4, 2    mat1[1][2] = 0.000000   mat2[2][4] = 1.000000   dest[1][4] = 0.000000
iterals: 1, 5, 0    mat1[1][0] = 0.000000   mat2[0][5] = 0.000000   dest[1][5] = 0.000000
iterals: 1, 5, 1    mat1[1][1] = 0.000000   mat2[1][5] = 0.000000   dest[1][5] = 0.000000
iterals: 1, 5, 2    mat1[1][2] = 0.000000   mat2[2][5] = 0.000000   dest[1][5] = 0.000000
iterals: 2, 0, 0    mat1[2][0] = 0.000000   mat2[0][0] = 0.000000   dest[2][0] = 0.000000
iterals: 2, 0, 1    mat1[2][1] = 20.000000  mat2[1][0] = 0.000000   dest[2][0] = 0.000000
iterals: 2, 0, 2    mat1[2][2] = 0.000000   mat2[2][0] = 0.000000   dest[2][0] = 0.000000
iterals: 2, 1, 0    mat1[2][0] = 0.000000   mat2[0][1] = 0.000000   dest[2][1] = 0.000000
iterals: 2, 1, 1    mat1[2][1] = 20.000000  mat2[1][1] = 0.000000   dest[2][1] = 0.000000
iterals: 2, 1, 2    mat1[2][2] = 0.000000   mat2[2][1] = 0.000000   dest[2][1] = 0.000000
iterals: 2, 2, 0    mat1[2][0] = 0.000000   mat2[0][2] = 0.000000   dest[2][2] = 0.000000
iterals: 2, 2, 1    mat1[2][1] = 20.000000  mat2[1][2] = 1.000000   dest[2][2] = 20.000000
iterals: 2, 2, 2    mat1[2][2] = 0.000000   mat2[2][2] = 0.000000   dest[2][2] = 20.000000
iterals: 2, 3, 0    mat1[2][0] = 0.000000   mat2[0][3] = 0.000000   dest[2][3] = 0.000000
iterals: 2, 3, 1    mat1[2][1] = 20.000000  mat2[1][3] = 0.000000   dest[2][3] = 0.000000
iterals: 2, 3, 2    mat1[2][2] = 0.000000   mat2[2][3] = 0.000000   dest[2][3] = 0.000000
iterals: 2, 4, 0    mat1[2][0] = 0.000000   mat2[0][4] = 0.000000   dest[2][4] = 0.000000
iterals: 2, 4, 1    mat1[2][1] = 20.000000  mat2[1][4] = 0.000000   dest[2][4] = 0.000000
iterals: 2, 4, 2    mat1[2][2] = 0.000000   mat2[2][4] = 1.000000   dest[2][4] = 0.000000
iterals: 2, 5, 0    mat1[2][0] = 0.000000   mat2[0][5] = 0.000000   dest[2][5] = 0.000000
iterals: 2, 5, 1    mat1[2][1] = 20.000000  mat2[1][5] = 0.000000   dest[2][5] = 0.000000
iterals: 2, 5, 2    mat1[2][2] = 0.000000   mat2[2][5] = 0.000000   dest[2][5] = 0.000000
iterals: 3, 0, 0    mat1[3][0] = 0.000000   mat2[0][0] = 0.000000   dest[3][0] = 0.000000
iterals: 3, 0, 1    mat1[3][1] = 0.000000   mat2[1][0] = 20.000000  dest[3][0] = 0.000000
iterals: 3, 0, 2    mat1[3][2] = 0.000000   mat2[2][0] = 0.000000   dest[3][0] = 0.000000
iterals: 3, 1, 0    mat1[3][0] = 0.000000   mat2[0][1] = 0.000000   dest[3][1] = 0.000000
iterals: 3, 1, 1    mat1[3][1] = 0.000000   mat2[1][1] = 0.000000   dest[3][1] = 0.000000
iterals: 3, 1, 2    mat1[3][2] = 0.000000   mat2[2][1] = 0.000000   dest[3][1] = 0.000000
iterals: 3, 2, 0    mat1[3][0] = 0.000000   mat2[0][2] = 0.000000   dest[3][2] = 0.000000
iterals: 3, 2, 1    mat1[3][1] = 0.000000   mat2[1][2] = 0.000000   dest[3][2] = 0.000000
iterals: 3, 2, 2    mat1[3][2] = 0.000000   mat2[2][2] = 0.000000   dest[3][2] = 0.000000
iterals: 3, 3, 0    mat1[3][0] = 0.000000   mat2[0][3] = 0.000000   dest[3][3] = 0.000000
iterals: 3, 3, 1    mat1[3][1] = 0.000000   mat2[1][3] = 0.000000   dest[3][3] = 0.000000
iterals: 3, 3, 2    mat1[3][2] = 0.000000   mat2[2][3] = 0.000000   dest[3][3] = 0.000000
iterals: 3, 4, 0    mat1[3][0] = 0.000000   mat2[0][4] = 0.000000   dest[3][4] = 0.000000
iterals: 3, 4, 1    mat1[3][1] = 0.000000   mat2[1][4] = 0.000000   dest[3][4] = 0.000000
iterals: 3, 4, 2    mat1[3][2] = 0.000000   mat2[2][4] = 1.000000   dest[3][4] = 0.000000
iterals: 3, 5, 0    mat1[3][0] = 0.000000   mat2[0][5] = 0.000000   dest[3][5] = 0.000000
iterals: 3, 5, 1    mat1[3][1] = 0.000000   mat2[1][5] = 0.000000   dest[3][5] = 0.000000
iterals: 3, 5, 2    mat1[3][2] = 0.000000   mat2[2][5] = 0.000000   dest[3][5] = 0.000000
iterals: 4, 0, 0    mat1[4][0] = 0.000000   mat2[0][0] = 0.000000   dest[4][0] = 0.000000
iterals: 4, 0, 1    mat1[4][1] = 0.000000   mat2[1][0] = 20.000000  dest[4][0] = 0.000000
iterals: 4, 0, 2    mat1[4][2] = 30.000000  mat2[2][0] = 0.000000   dest[4][0] = 0.000000
iterals: 4, 1, 0    mat1[4][0] = 0.000000   mat2[0][1] = 0.000000   dest[4][1] = 0.000000
iterals: 4, 1, 1    mat1[4][1] = 0.000000   mat2[1][1] = 0.000000   dest[4][1] = 0.000000
iterals: 4, 1, 2    mat1[4][2] = 30.000000  mat2[2][1] = 0.000000   dest[4][1] = 0.000000
iterals: 4, 2, 0    mat1[4][0] = 0.000000   mat2[0][2] = 0.000000   dest[4][2] = 0.000000
iterals: 4, 2, 1    mat1[4][1] = 0.000000   mat2[1][2] = 0.000000   dest[4][2] = 0.000000
iterals: 4, 2, 2    mat1[4][2] = 30.000000  mat2[2][2] = 0.000000   dest[4][2] = 0.000000
iterals: 4, 3, 0    mat1[4][0] = 0.000000   mat2[0][3] = 0.000000   dest[4][3] = 0.000000
iterals: 4, 3, 1    mat1[4][1] = 0.000000   mat2[1][3] = 0.000000   dest[4][3] = 0.000000
iterals: 4, 3, 2    mat1[4][2] = 30.000000  mat2[2][3] = 0.000000   dest[4][3] = 0.000000
iterals: 4, 4, 0    mat1[4][0] = 0.000000   mat2[0][4] = 0.000000   dest[4][4] = 0.000000
iterals: 4, 4, 1    mat1[4][1] = 0.000000   mat2[1][4] = 0.000000   dest[4][4] = 0.000000
iterals: 4, 4, 2    mat1[4][2] = 30.000000  mat2[2][4] = 0.000000   dest[4][4] = 0.000000
iterals: 4, 5, 0    mat1[4][0] = 0.000000   mat2[0][5] = 0.000000   dest[4][5] = 0.000000
iterals: 4, 5, 1    mat1[4][1] = 0.000000   mat2[1][5] = 0.000000   dest[4][5] = 0.000000
iterals: 4, 5, 2    mat1[4][2] = 30.000000  mat2[2][5] = 0.000000   dest[4][5] = 0.000000
iterals: 5, 0, 0    mat1[5][0] = 0.000000   mat2[0][0] = 0.000000   dest[5][0] = 0.000000
iterals: 5, 0, 1    mat1[5][1] = 0.000000   mat2[1][0] = 20.000000  dest[5][0] = 0.000000
iterals: 5, 0, 2    mat1[5][2] = 0.000000   mat2[2][0] = 0.000000   dest[5][0] = 0.000000
iterals: 5, 1, 0    mat1[5][0] = 0.000000   mat2[0][1] = 0.000000   dest[5][1] = 0.000000
iterals: 5, 1, 1    mat1[5][1] = 0.000000   mat2[1][1] = 0.000000   dest[5][1] = 0.000000
iterals: 5, 1, 2    mat1[5][2] = 0.000000   mat2[2][1] = 0.000000   dest[5][1] = 0.000000
iterals: 5, 2, 0    mat1[5][0] = 0.000000   mat2[0][2] = 0.000000   dest[5][2] = 0.000000
iterals: 5, 2, 1    mat1[5][1] = 0.000000   mat2[1][2] = 0.000000   dest[5][2] = 0.000000
iterals: 5, 2, 2    mat1[5][2] = 0.000000   mat2[2][2] = 0.000000   dest[5][2] = 0.000000
iterals: 5, 3, 0    mat1[5][0] = 0.000000   mat2[0][3] = 0.000000   dest[5][3] = 0.000000
iterals: 5, 3, 1    mat1[5][1] = 0.000000   mat2[1][3] = 0.000000   dest[5][3] = 0.000000
iterals: 5, 3, 2    mat1[5][2] = 0.000000   mat2[2][3] = 0.000000   dest[5][3] = 0.000000
iterals: 5, 4, 0    mat1[5][0] = 0.000000   mat2[0][4] = 0.000000   dest[5][4] = 0.000000
iterals: 5, 4, 1    mat1[5][1] = 0.000000   mat2[1][4] = 0.000000   dest[5][4] = 0.000000
iterals: 5, 4, 2    mat1[5][2] = 0.000000   mat2[2][4] = 0.000000   dest[5][4] = 0.000000
iterals: 5, 5, 0    mat1[5][0] = 0.000000   mat2[0][5] = 0.000000   dest[5][5] = 0.000000
iterals: 5, 5, 1    mat1[5][1] = 0.000000   mat2[1][5] = 0.000000   dest[5][5] = 0.000000
iterals: 5, 5, 2    mat1[5][2] = 0.000000   mat2[2][5] = 0.000000   dest[5][5] = 0.000000

这是源代码:

#include<stdio.h>

void prod(float mat1[][6], float mat2[][6], float dest[][6], int m1, int n1, int n2) {
     printf("orders: %d, %d, %d\n", m1, n1, n2);
     int i, j, k; // kmax = n1, jmax = n2, imax = m1
     for (i = 0; i < m1; i++) {
         for (j = 0; j < n2; j++) {
             dest[i][j] = 0;
             for (k = 0; k < n1; k++) {
                 dest[i][j] += mat1[i][k]*mat2[k][j];
                 printf("iterals: %d, %d, %d\t", i, j, k);
                 printf("mat1[%d][%d] = %f\t", i, k, mat1[i][k]);
                 printf("mat2[%d][%d] = %f\t", k, j, mat2[k][j]);
                 printf("dest[%d][%d] = %f\n", i, j, dest[i][j]);
             }
         }
     }
 }


void main() {
    float res[][6] = { 0 };

    float G[][6] =  { {10,  0,  0},
                      { 0,  0,  0},
                      { 0, 20,  0},
                      { 0,  0,  0},
                      { 0,  0, 30},
                      { 0,  0,  0} };

    float H[][6] =  { {1, 0, 0, 0, 0, 0},
                      {0, 0, 1, 0, 0, 0},
                      {0, 0, 0, 0, 1, 0} };

    prod(G, H, res, 6, 3, 6);
}

3 个答案:

答案 0 :(得分:2)

float res[][6] = { 0 };float res[1][6];,而不是您需要的float res[6][6];。如果没有完整的初始化程序列表,则需要写出所有尺寸。

void main()应该是int main(void)

您的G初始化列表也不适合float G[][6],它应该是float G[][3]

完整代码如下:https://ideone.com/2hz45m

答案 1 :(得分:1)

除了float res[][6] = { 0 };太小以外,其他@mch@Rishikesh Raje都说过,还存在其他问题。


mat1[][6], mat2[][6], dest[][6]视为mat1[a][b], mat2[b][c], dest[a][c],以获得更清晰,更灵活的代码。

在C99中以及在C11中(可选),参数可以是variable length array - VLA,从而简化了编码。
参见Passing a multidimensional variable length array to a function

void prod(int a, int b, int c, float mat1[a][b], float mat2[b][c], float dest[a][c]) {
  printf("orders: %d, %d, %d\n", a, b, c);
  for (int ai = 0; ai < a; ai++) {
    for (int ci = 0; ci < c; ci++) {
      float sum = 0.0;
      for (int bi = 0; bi < b; bi++) {
        sum += mat1[ai][bi] * mat2[bi][ci];
        printf("iterals: %d, %d, %d\t", ai, ci, bi);
        printf("mat1[%d][%d] = %2g\t", ai, bi, mat1[ai][bi]);
        printf("mat2[%d][%d] = %2g\t", bi, ci, mat2[bi][ai]);
        printf("dest[%d][%d] = %2g\n", ai, ci, sum);
      }
      dest[ai][ci] = sum;
    }
  }
}

#define A 6
#define B 3
#define C 6

int main(void) {
  float g[A][B] = { //
      {10, 0, 0}, //
      {0, 0, 0}, //
      {0, 20, 0}, //
      {0, 0, 0}, //
      {0, 0, 30}, //
      {0, 0, 0}};
  float h[B][C] = { // Note dimension change
      {1, 0, 0, 0, 0, 0}, //
      {0, 0, 1, 0, 0, 0}, //
      {0, 0, 0, 0, 1, 0}};
  float res[A][C]; // Note dimension change

  prod(A, B, C, g, h, res);
}

答案 2 :(得分:0)

  1. 您的G矩阵没有6列。它有3列,应定义如下

    float G[][3]
    
  2. 结果矩阵需要一个大小。

     float res[6][6] = { 0 };
    
  3. 您应将main定义为int main(void)

  4. 功能定义也应如下更改。

    void prod(float mat1[][3], float mat2[][6], float dest[][6], int m1, int n1, int n2) {