Hello,
What is the best and easiest way to transform a matrix?
With C# .NET4.8
// The best is with a sample i.e. --> Transpose the matrix from 3,5 to 5,3
namespace WindowsFormsAppMatrixBasic.Core
{
public static class ArrayExtensions
{
public static T[][] Transpose<T>(this T[][] source)
{
if (source.Length == 0 || source[0].Length == 0)
{
return Array.Empty<T[]>();
}
int rows = source.Length;
int cols = source[0].Length; // ???
T[][] result = new T[cols][];
for (int i = 0; i < cols; i++)
{
result[i] = new T[rows];
for (int j = 0; j < rows; j++)
{
result[i][j] = source[j][i];
}
}
return result;
}
}
public class MatrixTest
{
public void TestMatrix()
{
List<string> data = new List<string> {
"0000",
"0000",
"0000",
"FFFF",
"0000",
"0000",
"0000",
"0000",
"0000",
"0000",
"0000",
"0000",
"FFFF",
"0000",
"0000"
};
// ****** From a list, I should make a matrix depend of x and y values.
// Row Col x y State Counter Coding
// 1 1 1 10 10 1 32321 0000
// 2 2 1 20 10 1 32322 0000
// 3 3 1 30 10 1 32323 0000
// 4 4 1 40 10 0 32324 FFFF
// 5 5 1 50 10 1 32325 0000
// 6 1 2 10 110 1 32326 0000
// 7 2 2 20 110 1 32327 0000
// 8 3 2 30 110 1 32328 0000
// 9 4 2 40 110 1 32329 0000
// 10 5 2 50 110 1 32330 0000
// 11 1 3 10 210 1 32331 0000
// 12 2 3 20 210 1 32332 0000
// 13 3 3 30 210 0 32333 FFFF
// 14 4 3 40 210 1 32334 0000
// 15 5 3 50 210 1 32335 0000
string[,] matrix = new string[,]
{
{"0000", "0000", "0000", "FFFF", "0000"},
{"0000", "0000", "0000", "0000", "0000"},
{"0000", "0000", "FFFF", "0000", "0000"}
};
// Transpose the matrix from 3,5 to 5,3
string[,] transposed = matrix.Transpose() // ****** not work, however?


