红宝石 |向量分量()函数
component()是 Ruby 中的内置方法,返回向量中索引i处的元素。索引从零开始。
Syntax: vec1.component(i)
Parameters: The function accepts a single parameter
Return Value: It returns element at index i in the vector
示例 1 :
#Ruby program for component() method in Vector
#Include matrix
require "matrix"
#Initialize the vector
vec1
= Vector[1, 2]
i
= 1
#Prints element at index i
puts vec1.component(i)
输出:
2
示例 2 :
#Ruby program for component() method in Vector
#Include matrix
require "matrix"
#Initialize the vector
vec1
= Vector[1, 2, 3]
i
= 2
#Prints element at index i
puts vec1.component(i)
输出:
3