首页 > 解决方案 > 如何使用矩阵的不同幂生成绘图

问题描述

我有以下转换矩阵。

n <-10
A<-matrix(0,n,n)
diag(A[-1,]) <-0.5 
diag(A[,-1]) <-0.5 
A[1,n]<-0.5
A[n,1]<-0.5

如何通过产生 A 的不同幂并计算 1 范数来获得如下图? 在此处输入图像描述

标签: rmatrixplot

解决方案


要计算矩阵的幂,可以使用expm包或matrixcalc包:

A <- toeplitz(c(1,2,3)) # a square matrix
A
#       [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    2    1    2
# [3,]    3    2    1

library(expm)
A %^% 2
#       [,1] [,2] [,3]
# [1,]   14   10   10
# [2,]   10    9   10
# [3,]   10   10   14

library(matrixcalc)
matrix.power(A, 2)
#       [,1] [,2] [,3]
# [1,]   14   10   10
# [2,]   10    9   10
# [3,]   10   10   14

对于情节:

powers <- 0:8
Apowers <- lapply(powers, function(k) A %^% k)
norms <- sapply(Apowers, norm, type = "1")

plot(powers, norms)

推荐阅读