Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 968
yecid Avatar asked Dec 22 '22 10:12

yecid


1 Answers

Groovy lists have a transpose() method:

def transposed = mtrx.transpose()
like image 122
Rob Hruska Avatar answered Jan 04 '23 16:01

Rob Hruska