R-dims中不同参数值的特征值的persp图与对象的长度不匹配

R-dims中不同参数值的特征值的persp图与对象的长度不匹配,r,R,我是R的初学者,所以我的问题听起来可能很愚蠢。我有一个三维矩阵,它依赖于两个参数(a和b),我需要了解它的特征值是如何随着这些参数的不同值而变化的。基本上,我想用persp来绘制三个特征值(lambda1,lambda2,lambda3),以便了解它们何时为正,何时为负 矩阵为: a b a-b a+b b a a a b 其中-1

我是R的初学者,所以我的问题听起来可能很愚蠢。我有一个三维矩阵,它依赖于两个参数(a和b),我需要了解它的特征值是如何随着这些参数的不同值而变化的。基本上,我想用persp来绘制三个特征值(lambda1,lambda2,lambda3),以便了解它们何时为正,何时为负

矩阵为:

 a    b   a-b
a+b   b    a  
 a    a    b  
其中-1outer()
lambda1
解释为函数参数。我的建议是尝试类似的方法


C这就是你想要的:

require(akima) # akima package for easy surface interpolation
a <- seq(-1,1,0.01)
b <- seq(-1,1,0.01)
lambda1 <- function(a,b){eigen(matrix(c(a,b,a-b,a+b,b,a,a,a,b),3,3,byrow=T))$values[[1]]} 
# make it a data frame for all permutations to fix the issue that
# your current code runs for a, b vectors so you get the same result 200x
mytab<-data.frame(expand.grid(a=a,b=b))
# use adply and transform to generate the 1st eigenvalues for each
# takes a while for 200 x 200! you could reduce a & b
mytab<-mytab<-adply(mytab, 1, transform, c = lambda1(a, b))
# use interp from akima to create 40x40 matrix - you can change this
surface<-interp(mytab$a,mytab$b,mytab$c)
# plot the surface
persp(surface$x,surface$y,surface$z,col=ifelse(surface$z>=0,"red","white"))

# you can also create a spinning plot like this:

require(rgl)
plot3d(1,1,3,       # just an easy way to set the plot area
   xlab="a",
   ylab="b",
   zlab="lambda1")

surface3d(surface$x,surface$y,surface$z,
      col=ifelse(surface$z>=0,"red","white"), size=1)
require(akima)#akima软件包,便于曲面插值

a谢谢特洛伊,我想这正是我想要的。然而,我仍然无法理解我的代码中的错误。。。有没有办法在不依赖akima和plyr包的情况下获得相同的结果?嗨,我已经更新了它,以删除答案中的外部包依赖项。在代码方面,有几个问题。函数没有将a和b向量作为向量进行求值,因此它们对预期的a和b的任何组合都给出了相同的结果。因为它不是矢量化函数(即显式返回..[1]),“外部”函数不起作用。因此,上面的代码只是对a和b的每个组合进行了预计算,我们以persp()expects的形式创建了矩阵,但我意识到我在最后一点上没有那么清楚。如果显式运行核心lamda1函数:eigen(矩阵(c(mytab$a,mytab$b,mytab$a-mytab$b,mytab$a+mytab$b,mytab$b,mytab$a,mytab$a,mytab$b),3,3,byrow=T),您将看到它只返回1个结果而不是向量,因此您不能在需要向量化函数的函数中使用它。
dims [product 40401] do not match the length of object [1]
require(akima) # akima package for easy surface interpolation
a <- seq(-1,1,0.01)
b <- seq(-1,1,0.01)
lambda1 <- function(a,b){eigen(matrix(c(a,b,a-b,a+b,b,a,a,a,b),3,3,byrow=T))$values[[1]]} 
# make it a data frame for all permutations to fix the issue that
# your current code runs for a, b vectors so you get the same result 200x
mytab<-data.frame(expand.grid(a=a,b=b))
# use adply and transform to generate the 1st eigenvalues for each
# takes a while for 200 x 200! you could reduce a & b
mytab<-mytab<-adply(mytab, 1, transform, c = lambda1(a, b))
# use interp from akima to create 40x40 matrix - you can change this
surface<-interp(mytab$a,mytab$b,mytab$c)
# plot the surface
persp(surface$x,surface$y,surface$z,col=ifelse(surface$z>=0,"red","white"))

# you can also create a spinning plot like this:

require(rgl)
plot3d(1,1,3,       # just an easy way to set the plot area
   xlab="a",
   ylab="b",
   zlab="lambda1")

surface3d(surface$x,surface$y,surface$z,
      col=ifelse(surface$z>=0,"red","white"), size=1)
a <- seq(-1,1,0.05)
b <- seq(-1,1,0.05)
lambda1 <- function(a,b){eigen(matrix(c(a,b,a-b,a+b,b,a,a,a,b),3,3,byrow=T))$values[[1]]} 
mytab<-data.frame(expand.grid(a=a,b=b))

mytab$c<-apply(mytab, 1, function(x)lambda1(x["a"],x["b"]))

surface<-NULL
surface$x<-unique(mytab$a)
surface$y<-unique(mytab$b)
surface$z<-matrix(mytab$c,nrow=length(surface$x),byrow=TRUE) 

persp(surface$x,surface$y,surface$z,col=ifelse(surface$z>=0,"red","white"))