开发者

Transposing a matrix

I want to transpose a matrix, its a very easy task but its not working with me :

UPDATE

I am transposing the first matrix and storing it in a second one The two arrays point to the same structure I need two arrays (target and source) so I can display them later for comparison.

struct testing{
  int colmat1;
  int rowmat1;
  float mat[64][64];
};

int testtranspose(testing *test,testing *test2){
  int i,j;
  test2->colmat1 = test->rowmat1;
  test2->rowmat1 = test->colmat1
  for(i=0;i<test->rowmat1;i++){
    for(j=0;j<test->colmat1;j++){
      test2->mat[i][j] = test->mat[i][j];
    }
    printf("\n");
  }
}

I thought this is the correct method of doing it, but开发者_JS百科 apparently for a matrix such as :

1 2
3 4
5 6
7 8

I get :

1 2 0 0
3 4 0 0

What is the problem ?

Please help, Thanks !


To transpose the matrix, you need to change rows and columns. So you need to use:

targetMatrix[i][j] = sourceMatrix[j][i];

Note how the order of i,j is changed, since one matrix's rows are another's columns.

By the way, instead of (*a).b, you can write a->b. This is the normal way of accessing a field of a struct pointer.


Try this...

   struct testing{
  int colmat;
  int rowmat;
  float mat[64][64];
};

int testtranspose(testing *test,testing *test2){
  int i,j;
  test2->colmat = test->rowmat;
  test2->rowmat = test->colmat;
  for(i=0;i<test->rowmat;i++){
    for(j=0;j<test->colmat;j++){
      test2->mat[j][i] = test->mat[i][j];
    }
  }
  return 0;
}
int printmat(testing* mat)
{
    for(int i=0;i<mat->rowmat;i++)
    {
        printf("\n");
        for(int j=0;j<mat->colmat;j++)
            printf(("  %f"),mat->mat[i][j]);
    }
    return 0;
}

            // 2
// main.cpp
int _tmain(int argc, _TCHAR* argv[])
{
    testing mat1, mat2;
    memset(&mat1,0,sizeof(testing));
    memset(&mat2,0,sizeof(testing));
    mat1.colmat =2;
    mat1.rowmat =3;
    for(int i=0;i<mat1.rowmat;i++)
    {
        for(int j=0;j<mat1.colmat;j++)
            mat1.mat[i][j] = (float)rand();
    }
    printmat(&mat1);
    testtranspose(&mat1,&mat2);
    printmat(&mat2);
    getchar();

}


I am new to C / C++ (3rd day or so :) ) and I had the same problem. My approach was slightly different in that I thought it would be nice to have a function that would return a transposed matrix. Unfortunately, as I found out, you cannot return a an array nor pass an array to a function in C++ (let alone a double array), but you can pass / return a pointer which works similar to an array. So this is what I did:

int * matrix_transpose(int * A, int A_rows, int A_cols){
    int * B;
    int B_rows, B_cols;
    B_rows = A_cols; B_cols= A_rows;
    B = new int [B_rows*B_cols];
    for(int i=0;i<B_rows;i++){
        for(int j=0;j<B_cols;j++){
            B[i*B_cols+j]=A[j*A_cols+i];
        }
    }
    return B;
};

The trick was in dynamic arrays. I used A_rows and B_rows as separate names (you can use only rows and cols) in order to make the problem less intricate and less confusing when reading code.

B = new int [rows*cols] // This is cool in C++.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜