如何找到多语言矩阵的转置

矩阵的转置是通过交换原始矩阵的行和列获得的。在本文中,您将学习如何使用 C++、Python、JavaScript 和 C 查找正方形和矩形矩阵的转置。

问题陈述

你得到一个矩阵mat[][] 。您需要找到并打印矩阵的转置。

例子:

如何找到矩形矩阵的转置

  1. 矩形矩阵的转置顺序与原矩阵相反。例如,如果原始矩阵的阶数是 3 x 4,那么这个矩阵的转置阶数就是 4 x 3。
  2. 将原始矩阵的每一列存储为转置矩阵中的行,即 transposeMatrix[i][j] = mat[j][i]。

求矩形矩阵转置的 C++ 程序

下面是查找矩形矩阵转置的 C++ 程序:

 // C++ program to find the transpose of a rectangular Matrix
#include <iostream>
using namespace std;
// The order of the initial matrix is 3 x 4
#define size1 3
#define size2 4
// Function to transpose a Matrix
void transposeMatrix(int mat[][size2], int transposeMatrix[][size1])
{
for (int i=0; i<size2; i++)
{
for (int j=0; j<size1; j++)
{
transposeMatrix[i][j] = mat[j][i];
}
}
}
// Driver Code
int main()
{
int mat[size1][size2] = { {4, 2, 8, 2},
{9, 7, 1, 9},
{0, 2, 7, 5} };
cout << "Initial Matrix:" << endl;
// Printing the initial Matrix
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
// Variable to store the transposed Matrix
// The dimensions of transposedMatrix are opposite to that of mat
int transposedMatrix[size2][size1];
transposeMatrix(mat, transposedMatrix);
cout << "Transposed Matrix:" << endl;
// Printing the transposed Matrix
for (int i = 0; i < size2; i++)
{
for (int j = 0; j < size1; j++)
{
cout << transposedMatrix[i][j] << " ";
}
cout << endl;
}
return 0;
}

输出:

 Initial Matrix:
4 2 8 2
9 7 1 9
0 2 7 5
Transposed Matrix:
4 9 0
2 7 2
8 1 7
2 9 5

求矩形矩阵转置的 Python 程序

下面是用于查找矩形矩阵转置的 Python 程序:

 # Python program to find the transpose of a rectangular Matrix
# The order of the initial matrix is 3 x 4
size1 = 3
size2 = 4
# Function to transpose a Matrix
def transposeMatrix(mat, transposedMatrix):
for i in range(size2):
for j in range(size1):
transposedMatrix[i][j] = mat[j][i]
# Driver Code
mat = [ [4, 2, 8, 2],
[9, 7, 1, 9],
[0, 2, 7, 5] ]
print("Initial Matrix:")
# Printing the initial Matrix
for i in range(size1):
for j in range(size2):
print(mat[i][j], end=' ')
print()
# Variable to store the transposed Matrix
# The dimensions of transposedMatrix are opposite to that of mat
transposedMatrix = [[0 for x in range(size1)] for y in range(size2)]
transposeMatrix(mat, transposedMatrix)
print("Transposed Matrix:")
# Printing the transposed Matrix
for i in range(size2):
for j in range(size1):
print(transposedMatrix[i][j], end=' ')
print()

输出:

 Initial Matrix:
4 2 8 2
9 7 1 9
0 2 7 5
Transposed Matrix:
4 9 0
2 7 2
8 1 7
2 9 5

相关:如何在多种语言中找到两个数字的 LCM 和 GCD

用于查找矩形矩阵转置的 JavaScript 程序

下面是用于查找矩形矩阵转置的 JavaScript 程序:

 // JavaScript program to find the transpose of a rectangular Matrix
// The order of the initial matrix is 3 x 4
var size1 = 3
var size2 = 4
// Function to transpose a Matrix
function transposeMatrix(mat, transposeMatrix) {
for (let i=0; i<size2; i++) {
for (let j=0; j<size1; j++) {
transposeMatrix[i][j] = mat[j][i];
}
}
}
// Driver Code
var mat = [ [4, 2, 8, 2],
[9, 7, 1, 9],
[0, 2, 7, 5] ];
document.write("Initial Matrix:" + "<br>");
// Printing the initial Matrix
for (let i = 0; i < size1; i++) {
for (let j = 0; j < size2; j++) {
document.write(mat[i][j] + " ");
}
document.write("<br>");
}
// Variable to store the transposed Matrix
// The dimensions of transposedMatrix are opposite to that of mat1
var transposedMatrix = new Array(size2);
for (let k = 0; k < size2; k++) {
transposedMatrix[k] = new Array(size1);
}
transposeMatrix(mat, transposedMatrix);
document.write("Transposed Matrix:" + "<br>");
// Printing the transposed Matrix
for (let i = 0; i < size2; i++) {
for (let j = 0; j < size1; j++) {
document.write(transposedMatrix[i][j] + " ");
}
document.write("<br>");
}

输出:

 Initial Matrix:
4 2 8 2
9 7 1 9
0 2 7 5
Transposed Matrix:
4 9 0
2 7 2
8 1 7
2 9 5

相关:如何在 C++、Python 和 JavaScript 中交换两个变量

求矩形矩阵转置的 C 程序

下面是查找矩形矩阵转置的 C 程序:

 // C program to find the transpose of a rectangular Matrix
#include <stdio.h>
// The order of the initial matrix is 3 x 4
#define size1 3
#define size2 4
// Function to transpose a Matrix
void transposeMatrix(int mat[][size2], int transposeMatrix[][size1])
{
for (int i=0; i<size2; i++)
{
for (int j=0; j<size1; j++)
{
transposeMatrix[i][j] = mat[j][i];
}
}
}
// Driver Code
int main()
{
int mat[size1][size2] = { {4, 2, 8, 2},
{9, 7, 1, 9},
{0, 2, 7, 5} };
printf("Initial Matrix: ⁠n");
// Printing the initial Matrix
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
printf("%d ", mat[i][j]);
}
printf("⁠n");
}
// Variable to store the transposed Matrix
// The dimensions of transposedMatrix are opposite to that of mat1
int transposedMatrix[size2][size1];
transposeMatrix(mat, transposedMatrix);
printf("Transposed Matrix: ⁠n");
// Printing the transposed Matrix
for (int i = 0; i < size2; i++)
{
for (int j = 0; j < size1; j++)
{
printf("%d ", transposedMatrix[i][j]);
}
printf("⁠n");
}
return 0;
}

输出:

 Initial Matrix:
4 2 8 2
9 7 1 9
0 2 7 5
Transposed Matrix:
4 9 0
2 7 2
8 1 7
2 9 5

如何找到方阵的转置

  1. 方阵的转置顺序与原矩阵相同。例如,如果原始矩阵的阶数为 3 x 3,则该矩阵的转置阶数仍为 3 x 3。因此,声明一个与原始矩阵具有相同阶数的矩阵。
  2. 将原始矩阵的每一列存储为转置矩阵中的行,即 transposeMatrix[i][j] = mat[j][i]。

求方阵转置的 C++ 程序

下面是用于查找方阵转置的 C++ 程序:

 // C++ program to find the transpose of a square matrix
#include <iostream>
using namespace std;
// The order of the matrix is 3 x 3
#define size 3
// Function to transpose a Matrix
void transposeMatrix(int mat[][size], int transposeMatrix[][size])
{
for (int i=0; i<size; i++)
{
for (int j=0; j<size; j++)
{
transposeMatrix[i][j] = mat[j][i];
}
}
}

int main()
{
int mat[size][size] = { {4, 2, 8},
{9, 7, 1},
{0, 2, 7} };
cout << "Initial Matrix:" << endl;
// Printing the initial Matrix
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
// Variable to store the transposed Matrix
int transposedMatrix[size][size];
transposeMatrix(mat, transposedMatrix);
cout << "Transposed Matrix:" << endl;
// Printing the transposed Matrix
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout << transposedMatrix[i][j] << " ";
}
cout << endl;
}
return 0;
}

输出:

 Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7

查找方阵转置的 Python 程序

下面是寻找方阵转置的 Python 程序:

 # Python program to find the transpose of a square Matrix
# The order of the initial matrix is 3 x 3
size = 3
# Function to transpose a Matrix
def transposeMatrix(mat, transposedMatrix):
for i in range(size):
for j in range(size):
transposedMatrix[i][j] = mat[j][i]
# Driver Code
mat = [ [4, 2, 8],
[9, 7, 1],
[0, 2, 7] ]
print("Initial Matrix:")
# Printing the initial Matrix
for i in range(size):
for j in range(size):
print(mat[i][j], end=' ')
print()
# Variable to store the transposed Matrix
transposedMatrix = [[0 for x in range(size)] for y in range(size)]
transposeMatrix(mat, transposedMatrix)
print("Transposed Matrix:")
# Printing the transposed Matrix
for i in range(size):
for j in range(size):
print(transposedMatrix[i][j], end=' ')
print()

输出:

 Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7

相关:如何使用递归求自然数之和

用于查找方阵转置的 JavaScript 程序

下面是用于查找方阵转置的 JavaScript 程序:

 // JavaScript program to find the transpose of a square Matrix
// The order of the initial matrix is 3 x 3
var size = 3
// Function to transpose a Matrix
function transposeMatrix(mat, transposeMatrix) {
for (let i=0; i<size; i++) {
for (let j=0; j<size; j++) {
transposeMatrix[i][j] = mat[j][i];
}
}
}
// Driver Code
var mat = [ [4, 2, 8],
[9, 7, 1],
[0, 2, 7] ];
document.write("Initial Matrix:" + "<br>");
// Printing the initial Matrix
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
document.write(mat[i][j] + " ");
}
document.write("<br>");
}
// Variable to store the transposed Matrix
// The dimensions of transposedMatrix are opposite to that of mat1
var transposedMatrix = new Array(size);
for (let k = 0; k < size; k++) {
transposedMatrix[k] = new Array(size);
}
transposeMatrix(mat, transposedMatrix);
document.write("Transposed Matrix:" + "<br>");
// Printing the transposed Matrix
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
document.write(transposedMatrix[i][j] + " ");
}
document.write("<br>");
}

输出:

 Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7

求方阵转置的 C 程序

下面是查找方阵转置的 C 程序:

 // C program to find the transpose of a square Matrix
#include <stdio.h>
// The order of the initial matrix is 3 x 3
#define size 3
// Function to transpose a Matrix
void transposeMatrix(int mat[][size], int transposeMatrix[][size])
{
for (int i=0; i<size; i++)
{
for (int j=0; j<size; j++)
{
transposeMatrix[i][j] = mat[j][i];
}
}
}
// Driver Code
int main()
{
int mat[size][size] = { {4, 2, 8},
{9, 7, 1},
{0, 2, 7} };
printf("Initial Matrix: ⁠n");
// Printing the initial Matrix
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("%d ", mat[i][j]);
}
printf("⁠n");
}
// Variable to store the transposed Matrix
int transposedMatrix[size][size];
transposeMatrix(mat, transposedMatrix);
printf("Transposed Matrix: ⁠n");
// Printing the transposed Matrix
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("%d ", transposedMatrix[i][j]);
}
printf("⁠n");
}
return 0;
}

输出:

 Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7

解决基于矩阵的基本编程问题

矩阵是用于以结构化格式存储或显示数据的网格。矩阵在编程中被广泛用于执行各种操作。如果您希望涵盖所有编码面试基础,您必须知道如何在矩阵上执行基本运算,例如加法、减法、乘法等。