views:

131

answers:

2

Hello,

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 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 !

+4  A: 

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.

interjay
Thanks for replying. I just did : `((*test2).mat[i][j])=((*test).mat[j][i]); ` but this gave me an error in the code ..
ZaZu
@Zazu: don't surround the left side of the assignment with parantheses. Just use `test2->mat[i][j] = test->mat[j][i];`
interjay
Do I remove the first 2 lines of the code ? `test2->colmat2 = test->rowmat1; test2->rowmat2 = test->colmat1` -- I thought this is how I move contents of each row and col oppositely.
ZaZu
The code you posted won't compile since the `testing` struct doesn't have these fields. You still need these 2 lines, but you need to access the fields `colmat` and `rowmat`.
interjay
sorry I made a mistake copying the code here, colmat1 and rowmat1 ARE the ones in the structure -- Code updated
ZaZu
In that case you need these two lines: They are in charge of assigning the target matrix's size correctly. However, they won't rearrange the elements themselves - for that you need to do what I showed in my answer.
interjay
@interjay, you're right, The code worked !! I had to keep the `test2->colmat2 = test->rowmat1; test2->rowmat2 = test->colmat1` syntax, and reverse `[i][j]` for the source matrix ... it works !! Thank you very much for your time
ZaZu
+2  A: 

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();

}
Jujjuru
Thanks for the reply !! I see how this code works, and I understood it :) I implemented some parts but some others have commands I didnt hear of before (memset,printmat)..Thanks :)
ZaZu