Skip to content
Advertisement

How to swap rows and columns of a 2d array? [duplicate]

I’m trying to write a method for ‘transpose’ a two-dimensional array of integers, in which the rows and columns of the original matrix are exchanged.

However, I have no idea how I can realize this. How do I write out this method?

JavaScript

Advertisement

Answer

You could iterate over the rows and columns and assign each element [i,j] to the transposed [j,i]:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement