在 C++ 中转置矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14846905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transposing a matrix in C++
提问by Casper
I'm writing a program to transpose a given matrix using allocated memory. The function works perfect with square matrix NxN (rows==cols) but it crashes with MxN matrix (rows != cols). Please help
我正在编写一个程序来使用分配的内存转置给定的矩阵。该函数与方阵 NxN (rows==cols) 完美配合,但在 MxN 矩阵 (rows != cols) 时会崩溃。请帮忙
void transpose(int **matrix, int *row, int *col)
{
// dynamically allocate an array
int **result;
result = new int *[*col]; //creates a new array of pointers to int objects
// check for error
if (result == NULL)
{
cout << "Error allocating array";
exit(1);
}
for (int count = 0; count < *col; count++)
{
*(result + count) = new int[*row];
}
// transposing
for (int i = 0; i<*row; i++)
{
for (int j = i+1; j<*col; j++)
{
int temp = *(*(matrix + i) + j);
*(*(matrix + i) + j) = *(*(matrix + j) + i);
*(*(matrix + j) + i) = temp;
}
}
for (int i = 0; i<*row; i++)
{
for (int j = 0; j<*col; j++)
{
*(*(result + i) + j) = *(*(matrix + i) + j);
cout << *(*(result + i) + j) << "\t";
}
cout << endl;
}
}
回答by pippin1289
The lines:
线路:
for (int i = 0; i<*row; i++)
{
for (int j = i+1; j<*col; j++)
{
int temp = *(*(matrix + i) + j);
*(*(matrix + i) + j) = *(*(matrix + j) + i);
*(*(matrix + j) + i) = temp;
}
}
are the issue. The problem is that matrix is indexed by i then j, not j then i like you are doing in the second and third line in the while loop. Image that matrix is a 2x3 matrix, then you try to perform matrix[2][3] = matrix[3][2], but matrix[3][2] does not exist.
是问题。问题是矩阵由 i 然后 j 索引,而不是 j 然后 i 就像你在 while 循环的第二行和第三行所做的那样。想象矩阵是一个 2x3 矩阵,然后你尝试执行 matrix[2][3] = matrix[3][2],但是 matrix[3][2] 不存在。
It is best to go about simply initializing result directly in this loop:
最好直接在这个循环中简单地初始化结果:
for (int i = 0; i<*row; i++)
for (int j = 0; j<*col; j++)
result[j][i] = matrix[i][j];
Then you can output like below, or delete matrix and reassign matrix to be result as you wish. My entire transpose function became the following code (row and col need not be pointers to int pass by value is just fine. Also accessing matrices should use array subscripts as it is nicer style):
然后你可以像下面这样输出,或者删除矩阵并重新分配矩阵作为你想要的结果。我的整个转置函数变成了以下代码(row 和 col 不需要是指向 int 值传递的指针就可以了。访问矩阵也应该使用数组下标,因为它是更好的样式):
void transpose(int **matrix, int row, int col)
{
// dynamically allocate an array
int **result;
result = new int *[col]; //creates a new array of pointers to int objects
for (int i = 0; i < col; i++)
result[i] = new int[row];
// transposing
for (int i = 0; i<row; i++)
for (int j = 0; j<col; j++)
result[j][i] = matrix[i][j];
//output resulting matrix
for (int i = 0; i<col; i++) {
for (int j = 0; j<row; j++)
cout << result[i][j] << "\t";
cout << endl;
}
}
回答by borisbn
You are trying to transpose matrix "in place" :
您正在尝试“就地”转置矩阵:
((matrix + i) + j) = ((matrix + j) + i);
((矩阵 + i) + j) = ((矩阵 + j) + i);
you shouldn't do this. If count of columns is greater then count of rows, allocated for matrix
, you'll read and write non-allocated memory.
你不应该这样做。如果列数大于为 分配的行数matrix
,您将读写未分配的内存。
IMHO, It would be better to store whole matrix in continuous memory. Not in different pieces. In this manner the code would look like this:
恕我直言,最好将整个矩阵存储在连续内存中。不是在不同的部分。通过这种方式,代码将如下所示:
void transpose( int *matrix, int row, int col )
{
for ( int i = 0; i < row; i++ )
{
for ( int j = i + 1; j < col; j++ )
{
int temp = matrix[ i * col + j ];
matrix[ i * col + j ] = matrix[ j * col + i ];
matrix[ j * col + i ] = temp;
}
}
}
The only minus of this allocation, that you can't address an element like matrix[ i ][ j ]
but only matrix[ i + col + j ]
. Pluses are: 1) easy to allocate/deallocate memory (just matrix = new int[ col * row ]
and delete [] matrix
) 2) a little bit faster access to elements (because of continuous location of them)
这种分配的唯一缺点是,您无法处理像matrix[ i ][ j ]
but only 之类的元素matrix[ i + col + j ]
。优点是:1)容易分配/释放内存(只是matrix = new int[ col * row ]
和delete [] matrix
)2)更快地访问元素(因为它们的连续位置)
At the end, I think, that it would be the best way to look at std::vector
. If you want, I can show you, how will you function look with vector
最后,我认为,这将是看待std::vector
. 如果你愿意,我可以告诉你,你将如何使用矢量函数