Hi @Markus Freitag ,
I tried your code, it should be the problem that the format does not correspond, you can try the following code.
namespace WindowsFormsApp1.Core
{
public class ArrayExtensions<T>
{
public static T[,] Transpose(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;
}
}
public class MatrixTest
{
public void TestMatrix()
{
string[,] matrix = new string[3,5]
{
{"0000", "0000", "0000", "FFFF", "0000"},
{"0000", "0000", "0000", "0000", "0000"},
{"0000", "0000", "FFFF", "0000", "0000"}
};
var tMatrix = ArrayExtensions<string>.Transpose(matrix);
Console.WriteLine(tMatrix);
}
}
}
Edit:
File path:C:***\WindowsFormsApp1\bin\Debug\text.txt
public class MatrixTest
{
public void TestMatrix()
{
string[,] matrix = new string[3,5]
{
{"0000", "0000", "0000", "FFFF", "0000"},
{"0000", "0000", "0000", "0000", "0000"},
{"0000", "0000", "FFFF", "0000", "0000"}
};
// var tMatrix = ArrayExtensions<string>.Transpose(matrix);
var rows = matrix.GetLength(0);
var columns = matrix.GetLength(1);
using (TextWriter tw = new StreamWriter("text.txt"))
{
tw.WriteLine("Original Matrix");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
tw.Write(matrix[i, j] + " ");
}
tw.WriteLine();
}
tw.WriteLine("Transpose Matrix");
for (int j = 0; j < columns; j++)
{
for (int i = 0; i < rows; i++)
{
tw.Write(matrix[i, j] + " ");
}
tw.WriteLine();
}
}
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.