简体   繁体   English

R编程中的矩阵

[英]Matrix in R programming

learning R programming recently, here's a exercise I cannot figure it out.最近在学习 R 编程,这是一个我无法弄清楚的练习。

This program that asks the user for the 8 values of a 2 2x2 mathematical matrix.该程序要求用户输入 2 2x2 数学矩阵的 8 个值。 Another words, there are 4 values in one 2x2 matrix and another 4 values for the second matrix.换句话说,一个 2x2 矩阵中有 4 个值,第二个矩阵有另外 4 个值。 Then the program should has the user if they want to add, subtract, multiple or divided the 2 matrices.然后,如果用户想要对 2 个矩阵进行加、减、乘或除,程序应该有用户。 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.我是 R 编程的大牛,我的英语不太好,但我会尽力向你解释。

First, you can create matrices lik Amar did :首先,您可以像 Amar 那样创建矩阵:

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 ;您可以在矩阵上使用“传统”操作: + - * / 但是您必须知道对矩阵的元素逐一应用操作考虑 m3 = m1*m2 ; that means that m3[i,j] = m1[i,j]*m2[i,j]这意味着 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. Fo 除法不使用 %/% 运算,因为它不是除法而是模数。 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[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)如果你有方程 m6*m2 = m1 那么 m6 = m1*inverse(m2)

to inverse a matrix you have to install the package matlib :要反转矩阵,您必须安装包 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 :要对矩阵求逆,行列式应与 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:在 R 中,如果你有一个矩阵:

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函数中引用它,如下所示:

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要要求用户输入,请查看此 SO 答案: Creating a Prompt/Answer system to input data into R

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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