Ruby Matrix transpose() Method

Ruby Matrix transpose() Function with Examples

Ruby transpose() method is a matrix manipulation method and it is used to find the transpose of a given matrix. To find the transpose of a matrix we have to define a matrix first. But to define matrix you must have to include the ruby matrix library as require 'matrix'.

Ruby transpose() Method

Syntaxmat.transpose()
Where mat is the defined matrix, where we will find the transpose of that matrix.
ParametersThis method does not accept any parameters. It transposes directly to a given matrix.
Return valueReturns the transpose of the defined matrix.

Example Program

require 'matrix'
mat = Matrix[[1,4], [10,12]]
puts mat.transpose()

mat1 = Matrix[[1, 3, 8], [2, 0, 6], [11, 4, 9]]
puts mat1.transpose()

Output

Matrix[[1, 10], [4, 12]]
Matrix[[1, 2, 11], [3, 0, 4], [8, 6, 9]]
Posted in