- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Transpose() function in Ruby Programming
The transpose function in Ruby is mainly used to return the transpose of an array or a matrix.
Syntax
array.transpose
Or
Matrix.transpose
Let's take a couple of examples of the transpose function on array first and then on matrices.
Example 1
Consider the code shown below
# transpose() in array # array declaration first_arr = [[18, 22], [33, 3], [8, 6]] # array declaration second_arr = [[1, 3, 2, 5, 88, 9]] # print statements puts "transpose() output : #{first_arr.transpose()}
" puts "transpose() output : #{second_arr.transpose()}
"
Output
transpose() output : [[18, 33, 8], [22, 3, 6]] transpose() output : [[1], [3], [2], [5], [88], [9]]
Example 2
# transpose() in array # array declaration first_arr = [["xyz", "nil", "cat"]] # array declaration second_arr = [["donkey", "go", "lion"]] # print statements puts "transpose() output : #{first_arr.transpose()}
" puts "transpose() output : #{second_arr.transpose()}
"
Output
transpose() output : [["xyz"], ["nil"], ["cat"]] transpose() output : [["donkey"], ["go"], ["lion"]]
Now let's explore a few examples of transpose on matrices.
Example 3
# transpose() in Matrix require "matrix" # Initializing matrix matOne = Matrix[[5, 11], [1, 9]] # Prints the transpose matrix puts matOne.transpose()
Output
Matrix[[5, 1], [11, 9]]
Example 4
# transpose() in matrix require "matrix" # Initializing matrix matOne = Matrix[[1, 2], [6, 3], [4, 2]] # Printing matrix puts matOne.transpose()
Output
Matrix[[1, 6, 4], [2, 3, 2]]
- Related Articles
- Static Members in Ruby Programming
- Yield keyword in Ruby Programming
- True, False and Nil in Ruby Programming
- How does Recursion work in Ruby Programming?
- Array shift function in Ruby
- Array slice function in Ruby
- String reverse vs reverse! function in Ruby
- Anonymous function in Dart Programming
- table.pack() function in Lua programming
- table.unpack() function in Lua programming
- math.ceil() function in Lua programming
- math.floor() function in Lua programming
- math.max() function in Lua programming
- math.modf() function in Lua programming
- select() function in Lua programming

Advertisements