如何转置矩阵?
How to transpose matrix?
我用c#制作了8x8矩阵,现在需要转置矩阵。你能帮我转置矩阵吗?
这是我的矩阵
public double[,] MatriksT(int blok)
{
double[,] matrixT = new double[blok, blok];
for (int j = 0; j < blok ; j++)
{
matrixT[0, j] = Math.Sqrt(1 / (double)blok);
}
for (int i = 1; i < blok; i++)
{
for (int j = 0; j < blok; j++)
{
matrixT[i, j] = Math.Sqrt(2 / (double)blok) * Math.Cos(((2 * j + 1) * i * Math.PI) / 2 * blok);
}
}
return matrixT;
}
public double[,] Transpose(double[,] matrix)
{
int w = matrix.GetLength(0);
int h = matrix.GetLength(1);
double[,] result = new double[h, w];
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
result[j, i] = matrix[i, j];
}
}
return result;
}
public class Matrix<T>
{
public static T[,] TransposeMatrix(T[,] matrix)
{
var rows = matrix.GetLength(0);
var columns = matrix.GetLength(1);
var result = new T[columns, rows];
for (var c = 0; c < columns; c++)
{
for (var r = 0; r < rows; r++)
{
result[c, r] = matrix[r, c];
}
}
return result;
}
}
这就是它的称呼方式:
int[,] matris = new int[5, 8]
{
{1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
{9 , 10, 11, 12, 13, 14, 15, 16},
{17 , 18, 19, 20, 21, 22, 23, 24},
{25 , 26, 27, 28, 29, 30, 31, 32},
{33 , 34, 35, 36, 37, 38, 39, 40},
};
var tMatrix = Matrix<int>.TransposeMatrix(matris);
您的矩阵不是“完全正方形”矩阵。您需要一个结果 (transposedMatris) 矩阵。如果你的矩阵是一个完美的正方形,那么在同一个矩阵上交换 (a[i, j], a[j, i]) 就足够了。
泛型类型class有静态方法,方法调用如下。
int[,] transposedMatris = Matrix.TransposeMatrix<int>(matris);
dumpMatrix(transposedMatris);
将转置矩阵转储到控制台。
// Using Generics (dump string, integer, decimal, string)
public static void dumpMatrix<T>(T[,] a)
{
int m = a.GetLength(0);
int n = a.GetLength(1);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(a[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
Transposed matrix dump:
1 9 17 25 33
2 10 18 26 34
3 11 19 27 35
4 12 20 28 36
5 13 21 29 37
6 14 22 30 38
7 15 23 31 39
8 16 24 32 40
我不确定,但您需要按照以下步骤将图像顺时针旋转 90 度。
第 1 步:转置矩阵
第 2 步:水平翻转
int[,] a = new int[5, 5] {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
int m = a.GetLength(0);
int n = a.GetLength(1);
//dumpMatrix(a);
// Step 1: Transpose Matrix
int temp = 0;
for (int i = 0; i < m; i++)
{
for (int j = i; j < n; j++)
{
temp = a[i, j];
a[i, j] = a[j, i];
a[j, i] = temp;
}
}
//Console.WriteLine("Step 1: Transpose Matrix");
//dumpMatrix(a);
// Step 2: Flip horizontally
for (int i = 0; i < m; i++)
{
for (int j = 0; j < (n/2); j++)
{
temp = a[i, j];
a[i, j] = a[i, n - 1 - j];
a[i, n - 1 - j] = temp;
}
}
//Console.WriteLine("Step 2: Flip horizontally");
//dumpMatrix(a);
我用c#制作了8x8矩阵,现在需要转置矩阵。你能帮我转置矩阵吗? 这是我的矩阵
public double[,] MatriksT(int blok)
{
double[,] matrixT = new double[blok, blok];
for (int j = 0; j < blok ; j++)
{
matrixT[0, j] = Math.Sqrt(1 / (double)blok);
}
for (int i = 1; i < blok; i++)
{
for (int j = 0; j < blok; j++)
{
matrixT[i, j] = Math.Sqrt(2 / (double)blok) * Math.Cos(((2 * j + 1) * i * Math.PI) / 2 * blok);
}
}
return matrixT;
}
public double[,] Transpose(double[,] matrix)
{
int w = matrix.GetLength(0);
int h = matrix.GetLength(1);
double[,] result = new double[h, w];
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
result[j, i] = matrix[i, j];
}
}
return result;
}
public class Matrix<T>
{
public static T[,] TransposeMatrix(T[,] matrix)
{
var rows = matrix.GetLength(0);
var columns = matrix.GetLength(1);
var result = new T[columns, rows];
for (var c = 0; c < columns; c++)
{
for (var r = 0; r < rows; r++)
{
result[c, r] = matrix[r, c];
}
}
return result;
}
}
这就是它的称呼方式:
int[,] matris = new int[5, 8]
{
{1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
{9 , 10, 11, 12, 13, 14, 15, 16},
{17 , 18, 19, 20, 21, 22, 23, 24},
{25 , 26, 27, 28, 29, 30, 31, 32},
{33 , 34, 35, 36, 37, 38, 39, 40},
};
var tMatrix = Matrix<int>.TransposeMatrix(matris);
您的矩阵不是“完全正方形”矩阵。您需要一个结果 (transposedMatris) 矩阵。如果你的矩阵是一个完美的正方形,那么在同一个矩阵上交换 (a[i, j], a[j, i]) 就足够了。
泛型类型class有静态方法,方法调用如下。
int[,] transposedMatris = Matrix.TransposeMatrix<int>(matris);
dumpMatrix(transposedMatris);
将转置矩阵转储到控制台。
// Using Generics (dump string, integer, decimal, string)
public static void dumpMatrix<T>(T[,] a)
{
int m = a.GetLength(0);
int n = a.GetLength(1);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(a[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
Transposed matrix dump:
1 9 17 25 33
2 10 18 26 34
3 11 19 27 35
4 12 20 28 36
5 13 21 29 37
6 14 22 30 38
7 15 23 31 39
8 16 24 32 40
我不确定,但您需要按照以下步骤将图像顺时针旋转 90 度。
第 1 步:转置矩阵
第 2 步:水平翻转
int[,] a = new int[5, 5] { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25} }; int m = a.GetLength(0); int n = a.GetLength(1); //dumpMatrix(a); // Step 1: Transpose Matrix int temp = 0; for (int i = 0; i < m; i++) { for (int j = i; j < n; j++) { temp = a[i, j]; a[i, j] = a[j, i]; a[j, i] = temp; } } //Console.WriteLine("Step 1: Transpose Matrix"); //dumpMatrix(a); // Step 2: Flip horizontally for (int i = 0; i < m; i++) { for (int j = 0; j < (n/2); j++) { temp = a[i, j]; a[i, j] = a[i, n - 1 - j]; a[i, n - 1 - j] = temp; } } //Console.WriteLine("Step 2: Flip horizontally"); //dumpMatrix(a);