简体   繁体   English

通过C中的指针转置矩阵

[英]Transpose a matrix via pointer in C

I'm trying to transpose a matrix in C while passing the matrix to a function and return a pointer to a transposed matrix. 我正在尝试在C中将矩阵转置,同时将矩阵传递给函数并返回指向转置矩阵的指针。 What am I doing wrong in the second while loop? 在第二个while循环中我在做什么错?

in main 在主要

ptr = (float *) getMatrix(numRowsB, numColsB);
transposePtr = transpose(ptr, numRowsB, numColsB);
printf("\nBtranspose =\n");
printMatrix(transposePtr, numColsB, numRowsB);

create matrix 创建矩阵

float* getMatrix(int n, int m)
{
  int i, j;
  float *arrayPtr;

  if ((n <= 0) || (m <= 0))
  {
    printf("function getMatrix(): matrix dimensions are invalid\n");
    return NULL;
  }
  arrayPtr = (float *) malloc(n*m*sizeof(float));
  if(arrayPtr == NULL)
  {
    printf("function getMatrix: Unable to malloc array\n");
    return NULL;
  }

transpose function 转置功能

float* transpose(float *matrix, int n, int m)
{
    int i = 0;
    int j = 0;
    float num;
    float *transposed=(int*) malloc(sizeof(int)*n*m);
        while(i < n-1)
        {
            while(j < m-1)
            {
                num = *(matrix+i*m+j);
                *(transposed+j*m+i)= num;
                j++;
            }
            i++;
        }

    return transposed;
}

print fucntion 打印功能

 void print(float *matrix, int n, int m)
{
   int i = 0;//row counter
   int j = 0;//col counter
   for(i = 0; i < n; i++){
     printf("\n");
     for(j = 0; j < m; j++){
       printf("%f ", *(matrix + i*n + j));
     }
   }
}

Example input: 输入示例:

1  2  3

4  5  6

Output: 输出:

 1.000000 0.000000

 2.000000 3396.580087

-0.000000 0.000000

Part of the problem was your print function 问题的一部分是您的打印功能

Here is a version of your functions that works: 这是可以使用的功能版本:

float* transpose(float *matrix, int n, int m)
{
  int i = 0;
  int j = 0;
  float num;
  float *transposed=malloc(sizeof(float)*n*m);
  while(i < n) {
    j = 0;
    while(j < m) {
      num = *(matrix + i*m + j);
      *(transposed + i+n*j) = num; // I changed how you index the transpose
      j++;
    }
    i++;
  }

  return transposed;
}


void print(float *matrix, int n, int m)
{
  int i = 0;//row counter
  int j = 0;//col counter
  for(i = 0; i < n; i++){
    printf("\n");
    for(j = 0; j < m; j++){
      printf("%f ", *(matrix + i*m + j)); // Changed from n to m
    }
  }
}

There were a few things. 有几件事。

  1. Use sizeof(float) instead of sizeof(int) 使用sizeof(float)代替sizeof(int)

  2. Your loop should be i < n and j < m instead of i < n-1 and j < m-1 and finally you need to reset j to zero every time 您的循环应为i < nj < m而不是i < n-1j < m-1 ,最后您每次都需要将j重置为零

  3. The matrix indexes in inner most loop of your transpose function was incorrect 转置函数的最内层循环中的矩阵索引不正确

  4. Your print function was using the wrong variable in the multiplication 您的打印功能在乘法运算中使用了错误的变量

Also it is generally considered bad practice to cast the result of malloc in C . 同样, malloc的结果转换为C通常被认为是不好的做法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM