Click to See Complete Forum and Search --> : How to mirror a m*n matrix diagonally?
Oliver Kinne
May 25th, 1999, 03:20 AM
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?
Oliver Kinne
May 25th, 1999, 06:32 AM
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?
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;
}
}
yalcin@ioflex.com
Oliver Kinne
May 25th, 1999, 07:34 AM
Unfortunately, your solution only works for square matrices, i.e. m = n. I need one for m != n. But thanks for the attempt.
Oliver Kinne
May 25th, 1999, 07:36 AM
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.
muscicapa
May 25th, 1999, 08:03 AM
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 ! :-)
Oliver Kinne
May 25th, 1999, 08:08 AM
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. ;-)
If you can find the typo you will see that it will work for mxn also.
cheers.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.