r - Rcpp:检索和替换方阵的非对角线值
问题描述
使用 Rcpp/Armadillo,如何有效地提取/替换方阵的非对角线值?在 R 中,它可以使用:old_values = A[row(A) == (col(A) - k)]
; A[row(A) == (col(A) - k)] = new_values
. 使用 Armadillo,可以使用 for 循环(见下文)来实现此目标。但是有没有更简单的方法来编写代码?由于我需要对所有k
大矩阵(> 10000 行,> 10000 列)执行此操作,因此最好考虑效率。这是一个可重现的示例:
A = matrix(1:25, 5, 5)
A[row(A) == (col(A) - 3)] # extract the 3rd off-diagnal values
A[row(A) == (col(A) - 2)] = -5 of # replace the 2nd off-diagnal values with -5
使用 for 循环的 cpp 代码:
arma::vec retrieve_off_diag_values( arma::mat A, unsigned k )
{
unsigned n_cols = A.n_cols;
arma::vec off_diag_values(n_cols - k);
for( unsigned i=0; i <(n_cols - k); i++ )
{
off_diag_values(i) = A(i, i+k);
}
return off_diag_values;
}
解决方案
要将指定对角线中的值提取到向量中,其中 k < 0 表示次对角线,k = 0 表示主对角线,k > 0 表示超对角线:
#include<RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::vec diag_get(const arma::mat& X, int k) // note the 'const' and '&'
{
return X.diag(k);
}
要将对角线上的值设置为特定值:
// [[Rcpp::export]]
void diag_fill(arma::mat& X, int k, double value) // note the '&' character
{
X.diag(k).fill(value);
}
要将对角线上的特定值的实例更改为另一个值:
// [[Rcpp::export]]
void diag_change(arma::mat& X, int k, double old_value, double new_value)
{
X.diag(k).replace(old_value, new_value);
}
推荐阅读
- html - 弹性项目宽度总是等于图像内容的内在宽度
- c# - 无法删除第一个实体。我有一个包含多个联系人的客户联系人页面。如果我删除第一个联系人,它会显示 count == null 错误
- javascript - 在 MongoDB 中过滤数组和文档
- javascript - 如何 Mongoose 在查找特定字段时发送不同的值
- travis-ci - 如何在 Travis CI 中创建文件和写入上下文
- python - 出现错误:张量 a (1024) 的大小必须与非单维 3 的张量 b (512) 的大小相匹配
- global-payments-api - 我们可以使用通过链接支付 [POST] 方法启用保存卡选项吗
- java - 查找包含对象的数组的平均值(Java)
- python - Python:打印随机文本(取自数据库)并组合 2 个单词,如果我单击一个按钮?我已经有代码
- python - 使用 python 和 ngrok 制作 facebook bot,POST /webhook 500 内部服务器错误