#includeusing namespace std; int main() { int a[2][2] = {{1, 2}, {3, 4}}; int b[2][2]; // transpose a for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { b[i][j] = a[j][i]; } } // print b for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { cout << b[i][j] << " "; } cout << endl; } return 0; }
1 3 2 4
#include#include using namespace std; using namespace Eigen; int main() { MatrixXd a(2, 3); a << 1, 2, 3, 4, 5, 6; MatrixXd b = a.transpose(); cout << "a:\n" << a << endl; cout << "b:\n" << b << endl; return 0; }
a: 1 2 3 4 5 6 b: 1 4 2 5 3 6The second example uses the Eigen library, which is a C++ template library for linear algebra. The `MatrixXd` class represents a matrix of double precision floating point numbers. The `transpose()` function is a member function of the `MatrixXd` class and returns the transpose of a matrix.