Advertisement

R语言-矩阵

阅读量:

矩阵

每一个元素都相同的二维表,行列名可以更改

复制代码
    > mat1 <- matrix(c(1:12),nrow=3,ncol =4)
    > #定义了一个矩阵
    > mat1
     [,1] [,2] [,3] [,4]
    [1,]    1    4    7   10
    [2,]    2    5    8   11
    [3,]    3    6    9   12
    > dim(mat1)
    > #获得位数
    [1] 3 4

矩阵计算

复制代码
    > mat1*2
    > #矩阵与k(常数)相乘=全部元素×k
     [,1] [,2] [,3] [,4]
    [1,]    2    8   14   20
    [2,]    4   10   16   22
    [3,]    6   12   18   24
    
    > mat1+1
    > #R语言和matlab里的矩阵加常数k的计算都是矩阵里所有元素均加k
     [,1] [,2] [,3] [,4]
    [1,]    2    5    8   11
    [2,]    3    6    9   12
    [3,]    4    7   10   13
    
    > mat1
     [,1] [,2] [,3] [,4]
    [1,]    1    4    7   10
    [2,]    2    5    8   11
    [3,]    3    6    9   12
    > mat2 <- matrix(c(1:16),nrow=4)
    > #定义一个矩阵mat2
    > #矩阵A和B相乘的要求是A-col==B-row
    > mat2
     [,1] [,2] [,3] [,4]
    [1,]    1    5    9   13
    [2,]    2    6   10   14
    [3,]    3    7   11   15
    [4,]    4    8   12   16
    > mat1 %*% mat2
    > #%*%是矩阵相乘的计算符号
     [,1] [,2] [,3] [,4]
    [1,]   70  158  246  334
    [2,]   80  184  288  392
    [3,]   90  210  330  450

矩阵索引

复制代码
    > colnames(mat1) <- c('序号','参数1','参数2','结果')
    > #修改列名
    > mat1
     序号 参数1 参数2 结果
    [1,]    1     4     7   10
    [2,]    2     5     8   11
    [3,]    3     6     9   12
    
    > mat1[2,3]
    > #取出某个元素值
    参数2 
    8 
    > mat1[2,]
    > #取出第二行元素
     序号 参数1 参数2  结果 
    2     5     8    11 
    > mat1[,2]
    > #取出第二列
    [1] 4 5 6
    > mat1[c(1:2),c(2:4)]
    > 取出其中部分矩阵,第1-2行*第2-4列
     参数1 参数2 结果
    [1,]     4     7   10
    [2,]     5     8   11

元素筛选

复制代码
    #逻辑判断
    > mat1
     序号 参数1 参数2 结果
    [1,]    1     4     7   10
    [2,]    2     5     8   11
    [3,]    3     6     9   12
    
    > mat1[2,] >= 5
    > #第2行元素是否大于等于5
     序号 参数1 参数2  结果 
    FALSE  TRUE  TRUE  TRUE 
    
    > mat1[,3] >= 8
    > #第3列元素是否大于等于8
    [1] FALSE  TRUE  TRUE
    #想要取出TRUE的两行的数据
    > mat[mat1[,3] >= 8]
    > #默认数据放入了一个向量中
    [1]  2  3  5  6  8  9 11 12
    > mat[mat1[,3] >= 8,]
    > #mat1[,3] >= 8为真的个数是2所以后面的矩阵是两行,','表示按照默认格式
     序号 参数1 参数2 结果
    [1,]    2     5     8   11
    [2,]    3     6     9   12
    
    #which判断
    > mat[which(mat1[,4]>11)]
    [1] 3
    #mat1矩阵“结果”列里数据大于11的是第3列
    
    > mat[which(mat1[,4]>11),]
     序号 参数1 参数2  结果 
    3     6     9    12 
    #mat1矩阵“结果”列里数据大于11所在行的数据

Apply函数

Apply Functions to Array
Details
Returns a vector, array, or list of values obtained by applying a function to the margins of an array or matrix.
Usage
apply(X, MARGIN, FUN, ...)
The function part can be self-defined.
Arguments
X
An array (including a matrix).
MARGIN
A vector indicating the subscripts over which the function will be applied. For example, 1 refers to rows, 2 refers to columns, and c(1, 2) refers to both rows and columns. When X has named dimnames, it can be a character vector selecting dimension names.
FUN
The function to be applied: see 'Details'. For functions like +, %*%, etc., the function name must be backquoted or quoted.
...
Additional arguments passed to FUN.

复制代码
    > apply(mat,1,mean)
    > #对每一行取平均值
    [1] 5.5 6.5 7.5
    
    > apply(mat,2,mean)
    > 对每一列取平均值
     序号 参数1 参数2  结果 
    2     5     8    11

全部评论 (0)

还没有任何评论哟~