Ruby - Creating matrix from two vectors

Introduction

The following example creates a matrix from two vectors.

By passing vectors to the Matrix.columns() method, you construct a matrix whose rows are arrays of arrays.

Here the matrix has two columns created from the vectors v and v2, with each row containing two items, one from each column:

Demo

require "Matrix"    # This is essential! 
# ww w . jav  a2  s.co m
v = Vector[1,2,3,4,5] 
v2 = Vector[6,7,8,9,10] 
m4 = Matrix.columns([v,v2]) 
p( m4 )

Result

If you pass the same two vectors to the Matrix.rows() method, you would end up by creating a matrix that contains two rows, each of which is a vector:

Demo

require "Matrix"    # This is essential! 
# from  w w  w  . ja  v  a2s .  co m
v = Vector[1,2,3,4,5] 
v2 = Vector[6,7,8,9,10] 

m5 = Matrix.rows([v,v2]) 
p( m5 )

Result

Related Topic