As the title implies, how does one extract the sub- and superdiagonal of a matrix?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
回答2:
You may need to reshape the results....
help(lower.tri)
help(upper.tri)
help(diag)
upper.tri and lower.tri do not include the diagonals.