矩阵对角线相关计算1 - diag(), 取出, 修改 或 创建 对角线矩阵

使用diag()可以取出矩阵的对角线, 也可以修改对角线.
> x <- matrix(1:12, 3, 4)
> x
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

注意x是3行4列的, 所以对角线只有3个值. (第四列没有对应的对角).
> diag(x)
[1] 1 5 9

可以将值直接赋予给diag(x), 即修改对角线的值
> diag(x) <- 1:3
> x
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    2    8   11
[3,]    3    6    3   12

如果要把矩阵的对角线取出, 并且创建一个除对角线以外, 其他都为0的矩阵, 方法如下 : 
> x <- matrix(1:12,3,4)
> x
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> diag(diag(x), length(x[,1]), length(x[1,]) )
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    5    0    0
[3,]    0    0    9    0


使用diag(x)还可以创建对角线矩阵, 对角线外的元素默认为0.
例如, 创建一个对角线为1,3,9的矩阵. 
如下3行10列, 对角线为1,3,9, 其他为0

> diag(x=c(1,3,9), 3,10)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    0    0    0    0    0    0    0    0     0
[2,]    0    3    0    0    0    0    0    0    0     0
[3,]    0    0    9    0    0    0    0    0    0     0

以下对角线为1
> diag(x=1, 3,10)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    0    0    0    0    0    0    0    0     0
[2,]    0    1    0    0    0    0    0    0    0     0
[3,]    0    0    1    0    0    0    0    0    0     0

如果x的值和对角线长度不匹配, 则重复使用, 或少量使用.
例如 : 

> diag(x=c(1,2) , 8,10)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    0    0    0    0    0    0    0    0     0
[2,]    0    2    0    0    0    0    0    0    0     0
[3,]    0    0    1    0    0    0    0    0    0     0
[4,]    0    0    0    2    0    0    0    0    0     0
[5,]    0    0    0    0    1    0    0    0    0     0
[6,]    0    0    0    0    0    2    0    0    0     0
[7,]    0    0    0    0    0    0    1    0    0     0
[8,]    0    0    0    0    0    0    0    2    0     0
> diag(x=1:5 , 2,10)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    0    0    0    0    0    0    0    0     0
[2,]    0    2    0    0    0    0    0    0    0     0
> diag(x=1:5 , 8,10)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    0    0    0    0    0    0    0    0     0
[2,]    0    2    0    0    0    0    0    0    0     0
[3,]    0    0    3    0    0    0    0    0    0     0
[4,]    0    0    0    4    0    0    0    0    0     0
[5,]    0    0    0    0    5    0    0    0    0     0
[6,]    0    0    0    0    0    1    0    0    0     0
[7,]    0    0    0    0    0    0    2    0    0     0
[8,]    0    0    0    0    0    0    0    3    0     0


[参考]
> help(diag)

diag                   package:base                    R Documentation

Matrix Diagonals

Description:

     Extract or replace the diagonal of a matrix, or construct a
     diagonal matrix.

Usage:

     diag(x = 1, nrow, ncol)
     diag(x) <- value
     
Arguments:

       x: a matrix, vector or 1D array, or missing.

nrow, ncol: Optional dimensions for the result when ‘x’ is not a
          matrix.

   value: either a single value or a vector of length equal to that of
          the current diagonal.  Should be of a mode which can be
          coerced to that of ‘x’.

Details:

     ‘diag’ has four distinct usages:

       1. ‘x’ is a matrix, when it extracts the diagonal.

       2. ‘x’ is missing and ‘nrow’ is specified, it returns an
          identity matrix.

       3. ‘x’ is a scalar (length-one vector) and the only argument, it
          returns a square identity matrix of size given by the scalar.

       4. ‘x’ is a numeric vector, either of length at least 2 or there
          were further arguments.  This returns a matrix with the given
          diagonal and zero off-diagonal entries.

     It is an error to specify ‘nrow’ or ‘ncol’ in the first case.

Value:

     If ‘x’ is a matrix then ‘diag(x)’ returns the diagonal of ‘x’.
     The resulting vector will have ‘names’ if the matrix ‘x’ has
     matching column and rownames.

     The replacement form sets the diagonal of the matrix ‘x’ to the
     given value(s).

     In all other cases the value is a diagonal matrix with ‘nrow’ rows
     and ‘ncol’ columns (if ‘ncol’ is not given the matrix is square).
     Here ‘nrow’ is taken from the argument if specified, otherwise
     inferred from ‘x’: if that is a vector (or 1D array) of length two
     or more, then its length is the number of rows, but if it is of
     length one and neither ‘nrow’ nor ‘ncol’ is specified, ‘nrow =
     as.integer(x)’.

     When a diagonal matrix is returned, the diagonal elements are one
     except in the fourth case, when ‘x’ gives the diagonal elements:
     it will be recycled or truncated as needed, but fractional
     recycling and truncation will give a warning.

Note:

     Using ‘diag(x)’ can have unexpected effects if ‘x’ is a vector
     that could be of length one.  Use ‘diag(x, nrow = length(x))’ for
     consistent behaviour.

References:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_.  Wadsworth & Brooks/Cole.

See Also:

     ‘upper.tri’, ‘lower.tri’, ‘matrix’.

Examples:

     require(stats)
     dim(diag(3))
     diag(10, 3, 4) # guess what?
     all(diag(1:3) == {m <- matrix(0,3,3); diag(m) <- 1:3; m})
     
     diag(var(M <- cbind(X = 1:5, Y = stats::rnorm(5))))
     #-> vector with names "X" and "Y"
     
     rownames(M) <- c(colnames(M), rep("", 3));
     M; diag(M) #  named as well

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值