To perform scalar division on a matrix in R, you can use the division operator /
with a matrix and a scalar value.
In this example,
matrix1
using the matrix()
function. We provide a vector of values c(4, 8, 6, 10)
and specify the number of rows as 2.scalar
and set it to 2. This is the value by which each element of the matrix will be divided./
to divide matrix1
by scalar
. The result is stored in a new variable called resultMatrix1
.print()
function to display the resulting matrix resultMatrix1
.matrix1 <- matrix(c(4, 8, 6, 10), nrow = 2)
scalar <- 2
resultMatrix1 <- matrix1 / scalar
print(resultMatrix1)
[,1] [,2] [1,] 2 3 [2,] 4 5
In this example,
matrix2
using the matrix()
function. We provide a vector of values c(10, 20, 30, 40, 50, 60, 70, 80, 90)
and specify the number of rows as 3.scalar
and set it to 5. This is the value by which each element of the matrix will be divided./
to divide matrix2
by scalar
. The result is stored in a new variable called resultMatrix2
.print()
function to display the resulting matrix resultMatrix2
.matrix2 <- matrix(c(10, 20, 30, 40, 50, 60, 70, 80, 90), nrow = 3)
scalar <- 5
resultMatrix2 <- matrix2 / scalar
print(resultMatrix2)
[,1] [,2] [,3] [1,] 2 8 14 [2,] 4 10 16 [3,] 6 12 18
In this tutorial, we learned How to do Scalar Division on a Matrix in R language with well detailed examples.