Search code examples
arraysgroovymatrixtranspose

How can I transpose a matrix in Groovy?


I need transpose an array from rows into cols and cols into rows

def mtrx = [
   [1,2,3],
   [4,5,6]
]
//mtrx.anyMethod()
//expected result
//[[1,4],[2,5],[3,6]]

Do you know a direct method from it?

I don't know a Groovy method that transposes this case; if you know another way to transpose please tell me it.


Solution

  • Groovy lists have a transpose() method:

    def transposed = mtrx.transpose()