r - 如何使用矩阵的不同幂生成绘图
问题描述
我有以下转换矩阵。
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
解决方案
要计算矩阵的幂,可以使用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)
推荐阅读
- c# - C# 中的 Google 身份工具包 API
- html - 导航栏链接位置不在图像下方居中
- python - Python 使用预定义的类生成一些实例
- python - 字典中的值未排序
- arrays - 在 Julia 中初始化一个空数组,其中包含大量数组
- r - 在给定权重、波动率和相关矩阵的情况下计算 R 中的投资组合方差
- sql - 根据 Scala 中的另一列聚合数据框列中的数据
- reactjs - 无法获得 ref.current.offsetWidth
- python - Pre_delete 信号在 Django 中不起作用
- xml - CURL 和 Postman 为同一请求返回两个不同的结果