我想将矩阵的对角线元素设置为1,所以我使用diag()函数,但是出现错误。
aa=rand(3,3);
diag(aa)=ones(3)
Run Code Online (Sandbox Code Playgroud)
方法定义中的错误:必须显式导入函数LinAlg.diag才能进行扩展
我也尝试使用diag(aa)=[1,1,1],但似乎也不起作用。如何解决这个问题。
我想在Rcpp代码中使用“glasso”包,cpp代码如下:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
List sec(arma::mat x,double lam){
Environment gla("package:glasso");
Function gl=gla["glasso"];
double thr=1e-2;bool approx=0;
bool diag=1; bool nu=0;
List bc(5);
bc=gl(x,lam,nu,thr,thr,approx,approx,nu,nu,diag);
return(bc);}
Run Code Online (Sandbox Code Playgroud)
但是,当我在 R 中获取代码时,出现以下错误。
set.seed(100)
x<-matrix(rnorm(50*10),ncol=10)
s<- var(x)
library(glasso)
a<-sec(s, 0.01)
"Error in sec(s, 0.01) : Evaluation error: subscript out of bounds."
Run Code Online (Sandbox Code Playgroud)
我检查了“glasso”包的文档,结果列表包含 5 个值,所以我很困惑问题出在哪里。
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat fed(arma::mat x){
arma::mat zz=x.shed_rows(0,2);
return(zz);
}
Run Code Online (Sandbox Code Playgroud)
只想从矩阵中删除一些行,得到如下错误.从'void'转换为非标量类型'arma :: Mat}''
我是Rcpp用户,在我的cpp文件中,我需要重复使用矩阵。我想定义一个常数矩阵,但我不知道该怎么做。
我以前在Rcpp中定义了一个常量双精度类型变量,它对我来说很好用。但是当我对矩阵重复同样的方法时,
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::depends(RcppArmadillo)]]
const int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
// [[Rcpp::export]]
double tf(arma::mat x){
double aa=arma::sum(x+a);
return(aa);
}
Run Code Online (Sandbox Code Playgroud)
它具有以下错误