Como Encontrar a Transposição de uma Matriz em Vários Idiomas
A transposta de uma matriz é obtida trocando as linhas e colunas da matriz original. Neste artigo, você aprenderá como encontrar a transposição de uma matriz quadrada e retangular usando C ++, Python, JavaScript e C.
Declaração do Problema
Você recebe um tapete de matriz [] [] . Você precisa encontrar e imprimir a transposição da matriz.
Exemplos:
![](https://static1.makeuseofimages.com/wordpress/wp-content/uploads/2021/07/Transpose-of-a-matrix-1.png)
Como Encontrar a Transposição de uma Matriz Retangular
- A ordem de transposição de uma matriz retangular é oposta à da matriz original. Por exemplo, se a ordem da matriz original é 3 x 4, então a ordem da transposta dessa matriz seria 4 x 3.
- Armazene cada coluna da matriz original como linhas na matriz transposta, ou seja, transposeMatrix [i] [j] = mat [j] [i].
Programa C ++ para Encontrar a Transposição de uma Matriz Retangular
Abaixo está o programa C ++ para encontrar a transposta de uma matriz retangular:
// 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;
}
Saída:
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
Programa Python para encontrar a transposição de uma matriz retangular
Abaixo está o programa Python para encontrar a transposição de uma matriz retangular:
# 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()
Saída:
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
Programa JavaScript para encontrar a transposição de uma matriz retangular
Abaixo está o programa JavaScript para encontrar a transposição de uma matriz retangular:
// 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>");
}
Saída:
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
Programa C para Encontrar a Transposição de uma Matriz Retangular
Abaixo está o programa C para encontrar a transposta de uma matriz retangular:
// 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;
}
Saída:
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
Como Encontrar a Transposição de uma Matriz Quadrada
- A ordem de transposição de uma matriz quadrada é a mesma da matriz original. Por exemplo, se a ordem da matriz original é 3 x 3, a ordem da transposta dessa matriz ainda seria 3 x 3. Portanto, declare uma matriz com a mesma ordem da matriz original.
- Armazene cada coluna da matriz original como linhas na matriz transposta, ou seja, transposeMatrix [i] [j] = mat [j] [i].
Programa C ++ para encontrar a transposição de uma matriz quadrada
Abaixo está o programa C ++ para encontrar a transposição de uma matriz quadrada:
// 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;
}
Saída:
Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7
Programa Python para encontrar a transposição de uma matriz quadrada
Abaixo está o programa Python para encontrar a transposição de uma matriz quadrada:
# 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()
Saída:
Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7
Programa JavaScript para encontrar a transposição de uma matriz quadrada
Abaixo está o programa JavaScript para encontrar a transposição de uma matriz quadrada:
// 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>");
}
Saída:
Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7
Programa C para Encontrar a Transposição de uma Matriz Quadrada
Abaixo está o programa C para encontrar a transposição de uma matriz quadrada:
// 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;
}
Saída:
Initial Matrix:
4 2 8
9 7 1
0 2 7
Transposed Matrix:
4 9 0
2 7 2
8 1 7
Resolva problemas básicos de programação com base em matrizes
Uma matriz é uma grade usada para armazenar ou exibir dados em um formato estruturado. As matrizes são amplamente utilizadas na programação para realizar várias operações. Se você está procurando cobrir todas as bases de entrevista de codificação, você deve saber como realizar operações básicas como adição, subtração, multiplicação e muito mais em matrizes.