w3resource

R Programming: Create a matrix taking a given vector of numbers as input

R Programming: Matrix Exercise-2 with Solution

Write a R program to create a matrix taking a given vector of numbers as input. Display the matrix.

Sample Solution:

R Programming Code:

M = matrix(c(1:16), nrow = 4, byrow = TRUE)
print("Original Matrix:")
print(M)

Sample Output:

[1] "Original Matrix:"
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16                   

R Programming Code Editor:



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

Previous: Write a R program to create a blank matrix.
Next: Write a R program to create a matrix taking a given vector of numbers as input and define the column and row names. Display the matrix.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.