## R Program to create a matrix taking a given vector of numbers as input

### How to create a matrix taking a given vector of numbers as input

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,

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,dimnames = NULL) NA: An optional data vector. nrow: The desired number of rows. ncol: The desired number of columns. byrow: If FALSE (the default) the matrix is filled by columns, otherwise filled by rows. dimnames: NULL or a list of length 2 giving the row and column names respectively.

### How to create a matrix taking a given vector of numbers as input in the R Program

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.

### ALGORITHM

• STEP 1: Assign variable Matx with matrix values
• STEP 2: Create a matrix of 12 elements with 3 rows
• STEP 3: Create by calling like matrix(c(1:12), nrow = 3, byrow = TRUE)
• STEP 4: print the result matrix

### R Source Code

Matx = matrix(c(1:12), nrow = 3, byrow = TRUE)

print("Original Matrix is:")
##  "Original Matrix is:"
print(Matx)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12

### R Source Code

Matx = matrix(c(1:25), nrow = 5, byrow = TRUE)

print("Original Matrix is:")
##  "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