矩阵对角线相关计算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

相关文章
|
16天前
|
机器学习/深度学习 存储 人工智能
利用前缀和计算二维矩阵子矩阵的和
利用前缀和计算二维矩阵子矩阵的和
17 0
|
2月前
|
算法 前端开发 测试技术
3033. 修改矩阵
3033. 修改矩阵
14 0
|
2月前
|
人工智能
PTA- 矩阵的主对角线元素之和
矩阵的主对角线元素之和
15 1
|
2月前
|
机器学习/深度学习 人工智能
求一个3*3矩阵对角线元素之和
求一个3*3矩阵对角线元素之和。
25 1
|
3月前
|
算法
算法题—顺时针打印矩阵
算法题—顺时针打印矩阵
23 0
|
7月前
|
存储
第3章 数组与矩阵——3.3 矩阵元素的运算(1)
第3章 数组与矩阵——3.3 矩阵元素的运算(1)
|
7月前
第3章 数组与矩阵——3.3 矩阵元素的运算(2)
第3章 数组与矩阵——3.3 矩阵元素的运算(2)
|
10月前
|
网络架构 Python
矩阵各行元素之和
矩阵各行元素之和
61 0
|
11月前
判断上三角矩阵
判断上三角矩阵 (15 分)
95 0
|
11月前
R7-5 求矩阵各行元素之和
R7-5 求矩阵各行元素之和
75 0