简体   繁体   English

如何用另一个矩阵中的值替换C ++中矩阵的元素(使用Rcpp)?

[英]How to replace elements of a matrix in C++ with values from another matrix (using Rcpp)?

I have a small C++ function using Rcpp that replaces elements of one matrix with values from another matrix. 我有一个使用Rcpp的小C ++函数,它用一个来自另一个矩阵的值替换一个矩阵的元素。 It works fine for single cells, or a column as below: 它适用于单个单元格,或如下所示的列:

cppFunction('NumericMatrix changeC(NumericMatrix one, NumericMatrix two) {
NumericMatrix a = one;
NumericMatrix b = two;
b(_,1) = a(_,1);
return b;
}')

changeC(g,f)

If originally f is the following matrix: 如果最初f是以下矩阵:

      [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    6    6    6    6    6    6
[2,]    6    6    6    6    6    6
[3,]    6    6    6    6    6    6
[4,]    6    6    6    6    6    6
[5,]    6    6    6    6    6    6
[6,]    6    6    6    6    6    6

and g looks like the following matrix: 和g看起来像下面的矩阵:

        [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    5    5    5    5    5    5
 [2,]    5    5    5    5    5    5
 [3,]    5    5    5    5    5    5
 [4,]    5    5    5    5    5    5
 [5,]    5    5    5    5    5    5
 [6,]    5    5    5    5    5    5

When I run changeC(g,f) I end up with (as expected): 当我运行changeC(g,f)时,我最终得到(正如预期的那样):

      [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    6    5    6    6    6    6
[2,]    6    5    6    6    6    6
[3,]    6    5    6    6    6    6
[4,]    6    5    6    6    6    6
[5,]    6    5    6    6    6    6
[6,]    6    5    6    6    6    6

But what I really want to do is replace a subset of one matrix with a subset of another matrix from a different place (eg rows 1 to 3, columns 1 to 3 of one matrix (3*3) to rows 3 to 6, columns 3 to 6 (also 3*3) of the other matrix). 但我真正想要做的是将一个矩阵的子集替换为来自不同位置的另一个矩阵的子集(例如,行1到3,一个矩阵的列1到3(3 * 3)到行3到6,列另一个矩阵的3到6(也是3 * 3)。 I have tried: 我试过了:

cppFunction('NumericMatrix changeC(NumericMatrix one, NumericMatrix two) {
NumericMatrix a = one;
NumericMatrix b = two;
b( Range(0,2), Range(0,2)) = a( Range(3,5), Range(3,5));
return b;
}')

but this doesn't compile. 但这不编译。 Although: 虽然:

cppFunction('NumericMatrix changeC(NumericMatrix one, NumericMatrix two) {
NumericMatrix a = one;
NumericMatrix b = two;
b = a( Range(3,5), Range(3,5));
return b;
}')

does compile. 编译。 What am I doing wrong? 我究竟做错了什么? In RI would do the following: 在RI中将执行以下操作:

f[1:3,1:3] <- g[4:6,4:6] (but this is relatively slow with a very large matrix (hence Rcpp). f [1:3,1:3] < - g [4:6,4:6](但这对于非常大的矩阵(因此Rcpp)来说相对较慢)。

Thanks for any help in advance. 在此先感谢您的帮助。

EDIT 1 编辑1

After a bit of playing around I've managed to get my matrix to step east and west (and I assume it would be similar to north and south - possibly a two step approach for North East, North West??): 经过一段时间的游戏,我设法让我的矩阵向东和向西走(我认为它类似于北方和南方 - 可能是东北,西北的两步法):

func <- 'NumericMatrix eastC(NumericMatrix a) {
int acoln=a.ncol();
NumericMatrix out(a.nrow(),a.ncol()) ;
for (int j = 0;j < acoln;j++) {
if (j > 0) {
out(_,j) = a(_,j-1);
 } else {
  out(_,j) = a(_,0);
 }
 }
 return out ;
 }'
 cppFunction(func)

Any refinements would be welcome. 任何改进都是受欢迎的。 I would ideally like to leave the first column as zeros rather than column 0. Any ideas? 理想情况下,我希望将第一列保留为0而不是第0列。任何想法?

I don't think the Rcpp subMatrix allows for assignments that way. 我认为Rcpp subMatrix不允许这样的分配。

Take a look at using RcppArmadillo and Armadillo submatrix views 看看使用RcppArmadillo和Armadillo子矩阵视图

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace arma;

// [[Rcpp::export]]
mat example( mat m1, mat m2) {
  m1.submat( 0,0, 2,2) = m2.submat( 3,3, 5,5 );
  return m1;
}

/*** R
m1 <- matrix(1,6,6)
m2 <- matrix(-1,6,6)
example(m1, m2)
*/

> m1 <- matrix(1,6,6)

> m2 <- matrix(-1,6,6)

> example(m1, m2)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   -1   -1   -1    1    1    1
[2,]   -1   -1   -1    1    1    1
[3,]   -1   -1   -1    1    1    1
[4,]    1    1    1    1    1    1
[5,]    1    1    1    1    1    1
[6,]    1    1    1    1    1    1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM