I'm a biginner in R programming and I don't speak english very well, but I'll try to explain to you.
First, you can create matrices lik Amar did :
m1 <- matrix( rep(2,4) , ncol = 2) #rep(x,n) : repeat x n times
m2 <- matrix( c(2,3,5,6) , nrow = 2 , ncol = 2) #Personaly I prefer to precise the number of rows and columns
> m1
[,1] [,2]
[1,] 2 2
[2,] 2 2
> m2
[,1] [,2]
[1,] 2 5
[2,] 3 6
The operations
You can use "traditional" operations on matrices : + - * /
But you have to know that operations are applied on matrix's elements one by one
consider that m3 = m1*m2 ; that means that m3[i,j] = m1[i,j]*m2[i,j]
m3 <- m1*m2
[,1] [,2]
[1,] 4 10
[2,] 6 12
This is clearly not what is matrices multiplication in mathematics
N.B.: the classic addition (+) is correct
For matrices multiplication you have to use the operation %*%
> m4 <- m1%*%m2
> m4
[,1] [,2]
[1,] 10 22
[2,] 10 22
Fo division don't use the operation %/% because it's not division but modulus. and it returnus modulus applied to matrices elements one by one. m5 = m1%/%m2 means m5[i,j]=m1[i,j]%/%m2[i,j]
> m5 <- m1%/%m2
> m5
[,1] [,2]
[1,] 1 0
[2,] 0 0
Please note that in mathematics the division is not applied on matrices. If you have the equation m6*m2 = m1 then m6 = m1*inverse(m2)
to inverse a matrix you have to install the package matlib :
install.packages("matlib")
> m6 <- m1*inv(m2)
> m6
[,1] [,2]
[1,] -4 3.333333
[2,] 2 -1.333333
Important ! to inverse a matrix, the determinant should be different from 0 :
> det(m2)
[1] -3
> inv(m2)
[,1] [,2]
[1,] -2 1.6666667
[2,] 1 -0.6666667
> det(m1)
[1] 0
> inv(m1)
Error in Inverse(X, tol = sqrt(.Machine$double.eps), ...) :
X is numerically singular