Skip to main content

Array.transpose

transpose

Group: Selections

Documentation

Swaps the rows and columns of a matrix represented by an array of arrays.

Examples

Transpose an array of arrays.

      matrix = [[0, 1, 2].to_array, [3, 4, 5].to_array, [6, 7, 8].to_array].to_array
# +---+---+---+
# | 0 | 1 | 2 |
# +---+---+---+
# | 3 | 4 | 5 |
# +---+---+---+
# | 6 | 7 | 8 |
# +---+---+---+

transposed = [[0, 3, 6].to_array, [1, 4, 7].to_array, [2, 5, 8].to_array].to_array
# +---+---+---+
# | 0 | 3 | 6 |
# +---+---+---+
# | 1 | 4 | 7 |
# +---+---+---+
# | 2 | 5 | 8 |
# +---+---+---+

matrix.transposed == transposed
# => True

Errors

  • If the rows (subarrays) do not all have the same length, an Illegal_Argument error is raised.