简体   繁体   中英

Matrix in R programming

learning R programming recently, here's a exercise I cannot figure it out.

This program that asks the user for the 8 values of a 2 2x2 mathematical matrix. Another words, there are 4 values in one 2x2 matrix and another 4 values for the second matrix. Then the program should has the user if they want to add, subtract, multiple or divided the 2 matrices. Print the record.

Please see attached for the sample output.

Sample output

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

NB: 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

In R, if you have a matrix:

m1 <- matrix(c(2,2,2,2), ncol = 2)
m2 <- matrix(c(4,4,4,4), ncol = 2)  

and you want to add/subtract/divide/multiple the two you simply:

m1 + m2 
     [,1] [,2]
[1,]    6    6
[2,]    6    6

If you store the inputted values in a list, you can refer to it inside the matrix function as above:

matrix(user_input, ncol = 2)
#or
matrix(c(ui1, ui2, ui3, ui4), ncol = 2)

To ask for user-input, look at this SO answer: Creating a Prompt/Answer system to input data into R

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM