In R, you can create a ones matrix by using the matrix()
function and specifying the number of rows and columns. You can initialize all elements to one by providing the value '1' as the argument.
In this example,
matrix()
function to create a 3x3 ones matrix. The arguments nrow
and ncol
specify the number of rows and columns in the matrix, respectively.ones_mat
.ones_mat
to the console to verify the creation of the ones matrix.ones_mat <- matrix(1, nrow = 3, ncol = 3)
print(ones_mat)
[,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 [3,] 1 1 1
In this example,
matrix()
function to create a 2x4 ones matrix. The arguments nrow
and ncol
specify the number of rows and columns in the matrix, respectively.ones_mat
.ones_mat
to the console to verify the creation of the ones matrix.ones_mat <- matrix(1, nrow = 2, ncol = 4)
print(ones_mat)
[,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 1 1 1 1
In this tutorial, we learned How to Create Ones Matrix in R language with well detailed examples.