w3resource

R Programming: Create a Blank Matrix

R Programming: Matrix Exercise-1 with Solution

Write a R program to create a blank matrix.

Sample Solution:

R Programming Code:

# Create a blank matrix with 10 rows and 5 columns
m = matrix(, nrow = 10, ncol = 5)

# Print a message describing the matrix
print("Empty matrix of 10 rows and 5 columns:")

# Print the created empty matrix
print(m)

Output:

[1] "Empty matrix of 10 rows and 5 columns:"
      [,1] [,2] [,3] [,4] [,5]
 [1,]   NA   NA   NA   NA   NA
 [2,]   NA   NA   NA   NA   NA
 [3,]   NA   NA   NA   NA   NA
 [4,]   NA   NA   NA   NA   NA
 [5,]   NA   NA   NA   NA   NA
 [6,]   NA   NA   NA   NA   NA
 [7,]   NA   NA   NA   NA   NA
 [8,]   NA   NA   NA   NA   NA
 [9,]   NA   NA   NA   NA   NA
[10,]   NA   NA   NA   NA   NA                     

Explanation:

  • m = matrix(, nrow = 10, ncol = 5): Creates a blank matrix named m with 10 rows and 5 columns. The matrix is filled with NA (missing) values by default.
  • print("Empty matrix of 10 rows and 5 columns:"): Prints a descriptive message about the matrix.
  • print(m): Displays the contents of the matrix m, showing 10 rows and 5 columns filled with NA values.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: R programming Matrix Exercises Home.
Next: Write a R program to create a matrix taking a given vector of numbers as input. Display the matrix.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/matrix/r-programming-matrix-exercise-1.php