提取R中矩阵的子对角线和超对角线 [英] Extract sub- and superdiagonal of a matrix in R

查看:943
本文介绍了提取R中矩阵的子对角线和超对角线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,如何提取矩阵的次对角线和超对角线?

As the title implies, how does one extract the sub- and superdiagonal of a matrix?

推荐答案

使用diag.对于超对角线,只需丢弃最后一行和第一列.对于对角线,丢弃第一行,最后一列:

Using diag. For the superdiagonal, you just discard the last row and first column. For the subdiagonal, discard first row, last column:

m <- matrix(1:9,nrow=3)

> m
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> diag(m)
[1] 1 5 9
> diag(m[-nrow(m),-1])
[1] 4 8
> diag(m[-1,-ncol(m)])
[1] 2 6

这篇关于提取R中矩阵的子对角线和超对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆