Here we are explaining how to write an R program to create a matrix taking a given vector of numbers as input. Here we are using a built-in function matrix() for this conversion. This method helps to creates a matrix from the given set of values. The syntax of this function is,
Below are the steps used in the R program to create a matrix taking a given vector of numbers as input. In this R program, we directly give the values to built-in functions. And print the function result. Here we used variables Matx for assigning matrix.
Matx = matrix(c(1:12), nrow = 3, byrow = TRUE)
print("Original Matrix is:")
## [1] "Original Matrix is:"
print(Matx)
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
Matx = matrix(c(1:25), nrow = 5, byrow = TRUE)
print("Original Matrix is:")
## [1] "Original Matrix is:"
print(Matx)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 6 7 8 9 10
## [3,] 11 12 13 14 15
## [4,] 16 17 18 19 20
## [5,] 21 22 23 24 25