如何引用数字向量
How to cout NumericVectors?
我对 Rcpp 编程很陌生,所以我正在尝试新的东西来了解一切是如何工作的。我写了一个小程序来比较两个 NumericVector 和 match() 函数。我还想打印出输入矢量和输出,但它似乎不起作用,因为我没有取回矢量的条目,而是存储位置(或类似的东西)。我还没有找到任何适用于数字矢量的"打印"功能,但也许还有另一种方法?任何帮助将不胜感激。
这是我的代码:
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
IntegerVector vergl(NumericVector eins, NumericVector zwei){
IntegerVector out = match(eins, zwei);
cout << eins << endl;
cout << zwei << endl;
cout << match(eins, zwei) << endl;
return out;
}
一个小例子:
vergl(c(1,2,3),c(2,3,4))
输出:
> vergl(c(1,2,3),c(2,3,4))
0xa1923c0
0xa192408
0xb6d7038
[1] NA 1 2
谢谢。
Read gallery.rcpp.org/articles/using-rcout .
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector vergl(NumericVector eins, NumericVector zwei){
IntegerVector out = match(eins, zwei);
Rcout << as<arma::rowvec>(eins) << std::endl;
Rcout << as<arma::rowvec>(zwei) << std::endl;
Rcout << as<arma::rowvec>(out) << std::endl;
return out;
}
在 R 中:
vergl(c(1,2,3),c(2,3,4))
# 1.0000 2.0000 3.0000
#
# 2.0000 3.0000 4.0000
#
# nan 1.0000 2.0000
#
#[1] NA 1 2
请尝试此Rf_PrintValue(eins);
或Function print("print"); print(eins);
或类似方法,如示例中的display
函数所示。
例如,假设这是Rf_PrintValue.cpp:
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
IntegerVector vergl(NumericVector eins, NumericVector zwei) {
IntegerVector out = match(eins, zwei);
Rf_PrintValue(eins);
Rf_PrintValue(zwei);
Rf_PrintValue(match(eins, zwei));
Function print("print");
print(out);
Function display("display");
display("out", out);
return out;
}
/*** R
display <- function(name, x) { cat(name, ":n", sep = ""); print(x) }
vergl(c(1,2,3),c(2,3,4))
*/
我们可以像这样运行它。 前三个输出向量来自 Rf_PrintValue
语句,第四个来自 print
语句,第五个来自 display
函数,最后一个是 verg1
函数的输出:
> library(Rcpp)
> sourceCpp("Rf_PrintValue.cpp")
> display <- function(name, x) { cat(name, ":n", sep = ""); print(x) }
> vergl(c(1,2,3),c(2,3,4))
[1] 1 2 3
[1] 2 3 4
[1] NA 1 2
[1] NA 1 2
out:
[1] NA 1 2
[1] NA 1 2
已修订 更改了第二个解决方案并添加了一个示例。 还添加了display
示例。
相关文章:
- 用C++中的数字和条件填充向量
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何从向量或数组中选择最常见的数字?(前五名)C++
- 输入验证将数字放入向量中
- 如何在向量中某个数字之后重新计算进程
- 如何使用count_if计算向量中的可变数字范围
- 删除C++中向量中第一个最小的数字(并保持顺序)
- 如何将数字转换为整数向量?
- 如何在两个不同大小的向量中找到公共数字
- 如果我们从每个 Y 向量中选择一个值,则 X 数字的每个组合都可能
- Rcpp - 用二进制函数转换数字向量?
- 从数字向量生成随机数
- 比较两个排序的数字向量(性能优先)
- 如何有效地将数字矢量列表组合到一个大数字向量中
- 如何引用数字向量
- 将二进制整数的向量转换为数字向量
- 有效地将数字向量的每个元素与前一个元素进行比较
- 在Rcpp中分配一个大型的二维数字向量数组
- 将数字向量从基数转换为基数