CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 1999
    Location
    United Kingdom
    Posts
    136

    How to mirror a m*n matrix diagonally?

    I want to mirror a m*n matrix of doubles diagonally, like:

    1 2 3 1 4
    4 5 6 => 2 5
    3 6

    However, I don't want to allocate two matrices and copy one into the other. Instead, I want to have one double *matrix, which I interpret as a 2-dimensional matrix, i.e. matrix[a][b] = matrix[a*width+b]. This is due to memory restrictions, because the matrix I want to mirror may be huge (several MB).

    Any ideas or mathematical solutions?


  2. #2
    Join Date
    May 1999
    Location
    United Kingdom
    Posts
    136

    Re: How to mirror a m*n matrix diagonally?

    Maybe a little more explaining required:

    Although I want a 2-dimensional matrix, I allocate one chunk of data:

    double *matrix = new double[m*n];

    matrix[x][y] = matrix[x*n+y];

    i.e.:

    1 2 3
    4 5 6 = 1 2 3 4 5 6

    1 4
    2 5 = 1 4 2 5 3 6
    3 6

    Any clearer?


  3. #3
    Guest

    Re: How to mirror a m*n matrix diagonally?

    i am not sure if I understood the problem. however will this work?
    int temp;
    for(int i=0; i<n; i++)
    {
    for(int j=0; j<m; j++)
    {
    temp = matrix[i*n+j];
    matrix[i*n+j] = matrix[j*n+i];
    matrix[j*n+i] = temp;
    }
    }

    [email protected]


  4. #4
    Join Date
    May 1999
    Location
    United Kingdom
    Posts
    136

    Re: How to mirror a m*n matrix diagonally?

    Unfortunately, your solution only works for square matrices, i.e. m = n. I need one for m != n. But thanks for the attempt.


  5. #5
    Join Date
    May 1999
    Location
    United Kingdom
    Posts
    136

    Re: How to mirror a m*n matrix diagonally?

    Once more:

    Mirror matrix m*n diagonally, where m != n, i.e. NOT square matrix.

    1 2 3 1 4
    4 5 6 => 2 5
    3 6

    Matrix is stored as a 1-dimensional array, i.e.: double[] = {1, 2, 3, 4, 5, 6}

    After mirroring, it should be: double[] = {1, 4, 2, 5, 3, 6}

    Need generic solution for any m and n.


  6. #6
    Join Date
    May 1999
    Location
    13 N 77 E
    Posts
    183

    Re: How to mirror a m*n matrix diagonally?DONT

    Hi
    If you want to transpose a linear representation of a matrix, why do u want to copy it.

    Just change the subscript finding arithmetic to get the transpose. I am sure u can do it in a jiffy ! :-)


  7. #7
    Join Date
    May 1999
    Location
    United Kingdom
    Posts
    136

    Re: How to mirror a m*n matrix diagonally?DONT

    The thing is, I need to pass the array to a third-party GraphControl, which will plot the data onto the screen. Furthermore, depending on what the user selects, I need to display the array either one way or the other, thus I need to transpose it. The matrix basically represents a plane, i.e. for each x and y there is a height value (the double), and I want to display the plane either from one side or the other, i.e. in 2D.

    Sorry for being such a pain. ;-)


  8. #8
    Guest

    Re: How to mirror a m*n matrix diagonally?

    If you can find the typo you will see that it will work for mxn also.

    cheers.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured