R Program to create a blank matrix

How to create a blank matrix

Here we are explaining how to write an R program to create a blank matrix. 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 blank matrix in the R Program

Below are the steps used in the R program to create a blank matrix. 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 6 rows with 5 columns STEP 3: Create by calling like matrix(, nrow = 6, ncol = 5) STEP 4: print the result matrix

R Source Code

Matx=matrix(c(1:30), nrow = 6, ncol = 5)

print("Empty matrix of 6 rows and 5 columns:")
## [1] "Empty matrix of 6 rows and 5 columns:"
print(Matx)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    7   13   19   25
## [2,]    2    8   14   20   26
## [3,]    3    9   15   21   27
## [4,]    4   10   16   22   28
## [5,]    5   11   17   23   29
## [6,]    6   12   18   24   30

R Source Code

Matx=matrix(c(1:30), nrow = 5, ncol = 6)

print("Empty matrix of 6 rows and 5 columns:")
## [1] "Empty matrix of 6 rows and 5 columns:"
print(Matx)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    6   11   16   21   26
## [2,]    2    7   12   17   22   27
## [3,]    3    8   13   18   23   28
## [4,]    4    9   14   19   24   29
## [5,]    5   10   15   20   25   30