Skip to main content
Bumped by Community user
Bumped by Community user
edited title
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

C# - Optimized matrix Matrix transpose algorithm?

If iI have a List<List<T> like the following :

Name    city   id
Anna     NY     1
Joe      NJ     2
Name    City   Id
Anna     NY     1
Joe      NJ     2

How can iI transpose it efficiently in order to have :

Name    Anna     Joe
City    NY       NJ
id      1        2
Name    Anna     Joe
City    NY       NJ
Id      1        2

I tried the following

public static void MatrixTranspose<T>(ref List<List<T>> matrix)
{                
   var h = matrix.Count - 2;
   var w = matrix[0].Count - 1;

   for (var i = 0; i < h; ++i)
   {
      for (var j = i + 1; j < w; ++j)
      {
          var tmp = matrix[i][j];
          matrix[i][j] = matrix[j][i];
          matrix[j][i] = tmp;
       }
   }
}

That seems to be working on paper iI believe  ? But iI feel like i'mI'm doing too much permutations. Is there a way to optimize it  ? Also, is the use of ref correct here  ?

C# - Optimized matrix transpose algorithm?

If i have a List<List<T> like the following :

Name    city   id
Anna     NY     1
Joe      NJ     2

How can i transpose it efficiently in order to have :

Name    Anna     Joe
City    NY       NJ
id      1        2

I tried the following

public static void MatrixTranspose<T>(ref List<List<T>> matrix)
{                
   var h = matrix.Count - 2;
   var w = matrix[0].Count - 1;

   for (var i = 0; i < h; ++i)
   {
      for (var j = i + 1; j < w; ++j)
      {
          var tmp = matrix[i][j];
          matrix[i][j] = matrix[j][i];
          matrix[j][i] = tmp;
       }
   }
}

That seems to be working on paper i believe  ? But i feel like i'm doing too much permutations. Is there a way to optimize it  ? Also, is the use of ref correct here  ?

Matrix transpose algorithm

If I have a List<List<T> like the following :

Name    City   Id
Anna     NY     1
Joe      NJ     2

How can I transpose it efficiently in order to have :

Name    Anna     Joe
City    NY       NJ
Id      1        2

I tried the following

public static void MatrixTranspose<T>(ref List<List<T>> matrix)
{                
   var h = matrix.Count - 2;
   var w = matrix[0].Count - 1;

   for (var i = 0; i < h; ++i)
   {
      for (var j = i + 1; j < w; ++j)
      {
          var tmp = matrix[i][j];
          matrix[i][j] = matrix[j][i];
          matrix[j][i] = tmp;
       }
   }
}

That seems to be working on paper I believe? But I feel like I'm doing too much permutations. Is there a way to optimize it? Also, is the use of ref correct here?

Source Link

C# - Optimized matrix transpose algorithm?

If i have a List<List<T> like the following :

Name    city   id
Anna     NY     1
Joe      NJ     2

How can i transpose it efficiently in order to have :

Name    Anna     Joe
City    NY       NJ
id      1        2

I tried the following

public static void MatrixTranspose<T>(ref List<List<T>> matrix)
{                
   var h = matrix.Count - 2;
   var w = matrix[0].Count - 1;

   for (var i = 0; i < h; ++i)
   {
      for (var j = i + 1; j < w; ++j)
      {
          var tmp = matrix[i][j];
          matrix[i][j] = matrix[j][i];
          matrix[j][i] = tmp;
       }
   }
}

That seems to be working on paper i believe ? But i feel like i'm doing too much permutations. Is there a way to optimize it ? Also, is the use of ref correct here ?