question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked MarkusFreitag-0088 commented

C# Transpose a 2D matrix, looking for the best way

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?


https://files.fm/f/ufs626uwf
174535-how-i-get-the-info-rows-colums-from-matrix.png


dotnet-csharpdotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered MarkusFreitag-0088 commented

Hi @MarkusFreitag-0088,
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);
         }
     }
 }

174895-1.jpg
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();
                 }                
             }
         }
     }

175160-1.jpg
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.


1.jpg (99.7 KiB)
1.jpg (99.7 KiB)
1.jpg (99.7 KiB)
1.jpg (99.7 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks.
Looks good.

 public class MatrixVariant3
 {
     public void TestMatrix()
     {
         string[,] matrix = new string[3, 5]
         {
              {"0000", "0BB0", "0000", "FFFF", "0000"},
              {"0000", "0000", "0000", "0000", "0000"},
              {"0000", "0000", "FFFF", "0000", "0000"}
         };
         var tMatrix = ArrayExtensions<string>.Transpose(matrix);
    
         string help = "";
         var rows = tMatrix.GetLength(0);
         var columns = tMatrix.GetLength(1);
    
         for (int r = 0; r < rows; r++)
         {
             for (int c = 0; c < columns; c++)
             {
                 //help = string.Join(" ", tMatrix[r,c]);
                 help += tMatrix[r, c];
             }
             Console.WriteLine(help);
             help = "";
         }
    
    
         Console.WriteLine(tMatrix);
    
         //0000 0000 0000
         //0BB0 0000 0000
         //0000 0000 FFFF
         //FFFF 0000 0000
         //0000 0000 0000
    
     }
 }

How would you write this to a file then? Is ther a way with Join?


Original Matrix

Transpose matrix

0 Votes 0 ·
LanHuang-MSFT avatar image LanHuang-MSFT MarkusFreitag-0088 ·

Hi @MarkusFreitag-0088,
You want to write the matrix to a file, you can use StreamWriter,
use Write instead of WriteLine when writing,
and you can write directly, no need for Transpose method, the specific code I have edited into the answer.
Best regards,
Lan Huang

1 Vote 1 ·

Thank you!

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered MarkusFreitag-0088 published

what is wrong with the result? the code and debug values look correct.


  {
      {"0000", "0000", "0000", "FFFF", "0000"},
      {"0000", "0000", "0000", "0000", "0000"},
      {"0000", "0000", "FFFF", "0000", "0000"}
  }
    
 //transposed:
    
  {
      {"0000", "0000", "0000"},
      {"0000", "0000", "0000"},
      {"0000", "0000", "FFFF"},
      {"FFFF", "0000", "0000"},
      {"0000", "0000", "0000"}
  }




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,

Can you show me how you managed that?

  // 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


How do you find the 5.3 matrix from the list. How can I solve this well?
Thanks for the sample in advance.

 // Transpose the matrix from 5,3 to 3,5   (each way)
      string[,] transposed = matrix.Transpose()  // *** not work, maybe the matrix is wrong.




0 Votes 0 ·