- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 Rcpp::CharacterMatrix
并将每一行转换为 Rcpp::List
中它自己的元素。
但是,我为此编写的函数有一个奇怪的行为,即列表的每个条目都对应于矩阵的最后一行。为什么会这样?这是一些与指针相关的概念吗?请解释。
功能:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List char_expand_list(CharacterMatrix A) {
CharacterVector B(A.ncol());
List output;
for(int i=0;i<A.nrow();i++) {
for(int j=0;j<A.ncol();j++) {
B[j] = A(i,j);
}
output.push_back(B);
}
return output;
}
测试矩阵:
这是传递给上述函数的矩阵A
。
mat = structure(c("a", "b", "c", "a", "b", "c", "a", "b", "c"), .Dim = c(3L, 3L))
mat
# [,1] [,2] [,3]
# [1,] "a" "a" "a"
# [2,] "b" "b" "b"
# [3,] "c" "c" "c"
输出:
上面的函数应该将这个矩阵作为输入并返回矩阵的行列表,如下所示:
char_expand_list(mat)
# [[1]]
# [1] "a" "a" "a"
#
# [[2]]
# [1] "b" "b" "b"
#
# [[3]]
# [1] "c" "c" "c"
但是我得到了不同的东西:
char_expand_list(mat)
# [[1]]
# [1] "c" "c" "c"
#
# [[2]]
# [1] "c" "c" "c"
#
# [[3]]
# [1] "c" "c" "c"
可以看出,输出有最后一个元素,例如“c”的矩阵行,对第一个和第二个列表元素重复。为什么会这样?
最佳答案
这里发生的事情很大程度上是 Rcpp 对象工作方式的结果。特别是,CharacterVector
充当指向内存位置的指针。通过在 for
之外定义此内存位置循环,结果是一个“全局”指针。也就是说,当更新到 B
发生在循环中这随后更新了 B
的所有变体已方便地存储在 Rcpp::List
中.因此,"c"
的重复行自始至终名单。
话虽如此,使用 .push_back()
是一个非常、非常、非常糟糕的想法。在任何 Rcpp
数据类型,因为您最终将在不断扩展的对象之间来回复制。复制将发生,因为 Rcpp 数据类型隐藏了底层 SEXP
控制必须重新创建的 R 对象。因此,您应该尝试以下方法之一:
Rcpp::CharacterVector
被创建在第一个 for
内循环和预分配 Rcpp::List
空间。std::list
与 std::vector<T>
输入 T
(即 std::string
)Rcpp::wrap(x)
从 Rcpp::List
返回正确的对象或修改函数返回类型至 std::list<std::vector<T> >
.Rcpp::List
空间与使用std::vector<T>
输入 T
(即 std::string
)。Rcpp::List
空间并制作一个clone()
将 Rcpp 对象存储在列表中之前。在这里,我们通过移动 B
的声明来重新排列函数。进入第一次循环,预分配列表空间,正常访问输出列表。
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
Rcpp::List char_expand_list_rearrange(Rcpp::CharacterMatrix A) {
Rcpp::List output(A.nrow());
for(int i = 0; i < A.nrow(); i++) {
Rcpp::CharacterVector B(A.ncol());
for(int j = 0; j < A.ncol(); j++) {
B[j] = A(i, j);
}
output[i] = B;
}
return output;
}
这里我们删除了Rcpp::CharacterVector
赞成std::vector<std::string>
并替换为 Rcpp::List
对于 std::list<std::vector<std::string> >
.最后,我们将标准对象转换为 Rcpp::List
。通过Rcpp::wrap()
.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
Rcpp::List char_expand_std_to_list(Rcpp::CharacterMatrix A) {
std::vector<std::string> B(A.ncol());
std::list<std::vector<std::string> > o;
for(int i = 0 ;i < A.nrow(); i++) {
for(int j = 0; j < A.ncol(); j++) {
B[j] = A(i, j);
}
o.push_back(B);
}
return Rcpp::wrap(o);
}
给予:
mat = structure(c("a", "b", "c", "a", "b", "c", "a", "b", "c"), .Dim = c(3L, 3L))
char_expand_std_to_list(mat)
# [[1]]
# [1] "a" "a" "a"
#
# [[2]]
# [1] "b" "b" "b"
#
# [[3]]
# [1] "c" "c" "c"
或者,您可以旨在保留 Rcpp::List
, 但只需声明大小它提前期待并且仍然使用std::vector<T>
元素。
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
Rcpp::List char_expand_list_vec(Rcpp::CharacterMatrix A) {
std::vector<std::string> B(A.ncol());
Rcpp::List o(A.nrow());
for(int i = 0; i < A.nrow(); i++) {
for(int j = 0; j < A.ncol(); j++) {
B[j] = A(i, j);
}
o[i] = B;
}
return o;
}
最后,通过为列表预定义的空间,有一个明确的克隆每次迭代的数据。
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
Rcpp::List char_expand_list_clone(Rcpp::CharacterMatrix A) {
Rcpp::CharacterVector B(A.ncol());
Rcpp::List output(A.nrow());
for(int i = 0; i < A.nrow(); i++) {
for(int j = 0; j < A.ncol(); j++) {
B[j] = A(i, j);
}
output[i] = clone(B);
}
return output;
}
基准测试结果表明,选项 1 具有重新排列和预分配空间表现最好。亚军是选项 4,它涉及在将每个 vector 保存到 Rcpp::List
之前克隆每个 vector .
library("microbenchmark")
library("ggplot2")
mat = structure(c("a", "b", "c", "a", "b", "c", "a", "b", "c"), .Dim = c(3L, 3L))
micro_mat_to_list =
microbenchmark(char_expand_list_rearrange(mat),
char_expand_std_to_list(mat),
char_expand_list_vec(mat),
char_expand_list_clone(mat))
micro_mat_to_list
# Unit: microseconds
# expr min lq mean median uq max neval
# char_expand_list_rearrange(mat) 1.501 1.9255 3.22054 2.1965 4.8445 6.797 100
# char_expand_std_to_list(mat) 2.869 3.2035 4.90108 3.7740 6.4415 27.627 100
# char_expand_list_vec(mat) 1.948 2.2335 3.83939 2.7130 5.2585 24.814 100
# char_expand_list_clone(mat) 1.562 1.9225 3.60184 2.2370 4.8435 33.965 100
关于c++ - 将 Rcpp 对象分配到 Rcpp 列表中会产生最后一个元素的拷贝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37502121/
我在 Chrome 上做了一些测试,requestAnimationFrame 产生了 61 fps 而 setTimeOut( callback, 0 ) 产生了 233 fps。 如果一个人想要超
当我调试代码时,我发现 GCC 和 Clang 都为 0.0/0.0 产生 nan,这是我所期望的,但 GCC 产生的 nan 将符号位设置为 1,而Clang 将其设置为 0(如果我没记错的话,与
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我在 R Studio 中有一个时间序列。现在我想计算这个系列的log()。我尝试了以下方法: i <- (x-y) ii <- log(i) 但是我得到以下信息:Warning message: I
我有兴趣了解 JavaScript 的内部结构.我试图阅读 SpiderMonkey 的来源和 Rhino但是绕过我的头是相当复杂的。 我问的原因是:为什么像 (![]+[])[+!![]+[]] 生
我们在 Delphi 中使用标准 TWebbrowser 组件,该组件在内部使用 mshtml.dll。另外,我们使用注册表来确保页面使用新的渲染引擎( Web-Browser-Control-Spe
我必须实现一个序列化/反序列化类,并且我正在使用 System.Xml.Serialization 。我有一些IList类型属性并希望在 IList 中序列化解码属于具有特定区域性信息的列表的所有十进
我有一个 Java 应用程序,它读取包含 SQL 查询的 JSON 文件,并使用 JDBC 在数据库上触发它们。 现在我有 5 万个这样的文件,我需要生成 5 万个独立线程来读取每个文件并将它们上传到
我正在尝试将 TensorFlow 入门页面上的示例线性回归程序调整为二次回归。为此,我只是添加了另一个变量并更改了函数。然而,这似乎会导致 NaN 值。这是我的代码: import numpy as
申请后KernelPCA到我的数据并将其传递给分类器 ( SVC ) 我收到以下错误: ValueError: Input contains NaN, infinity or a value too
这背后的想法是,如果我的数据库中存在登录名(正确的用户名+密码),我将重定向到一个页面,并且在进行此身份验证后,他们可以将消息存储在文本文件中。代码非常简单尽管我不确定为什么会收到 IllegalSt
我有一个返回 log10 值的函数。在将它们转换为正常数字时,出现溢出错误。 OverflowError: (34, 'Numerical result out of range') 我检查了日志值,
nosetests 抛出一个 ImportError,尽管我认为这是一个正确配置的 virtualenv。 ==============================================
我是这个网站的新手,所以如果我做错了什么,我提前道歉。当我尝试使用 kivy-garden 的 ScrollLabel 时,它给了我一个错误。基本上我正在尝试创建一个控制台日志,并且我需要能够在文本框
任何人都对 MDSJ 有任何经验?以下输入仅产生 NaN 结果,我不明白为什么。文档非常稀少。 import mdsj.Data; import mdsj.MDSJ; public class MDS
我有一个非常简单的 scala jcuda 程序,它添加了一个非常大的数组。一切都编译和运行得很好,直到我想从我的设备复制超过 4 个字节到主机。当我尝试复制超过 4 个字节时,我收到 CUDA_ER
我正在使用 Hero 组件在两个页面之间创建动画。Hero 组件用于包装一个 Image 小部件(没问题)和一个 Container 小部件(有问题)。 抛出以下溢出错误: ══╡ EXCEPTIO
我无法理解页面 https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/void 中的这一段: This ope
当在 Angular 中使用不立即触发事件的异步管道时(http 请求或任何有延迟的可观察对象),第一个值为 null为什么会这样?如何避免这种情况? 第一个变化: SimpleChange {
如果一个导入的库生成了一个会 panic 的 goroutine 怎么办?在这种情况下,开发人员无法阻止程序退出。 就像在这段代码中一样,使用延迟恢复调用一个错误的库没有帮助,因为该库正在生成一个 p
我是一名优秀的程序员,十分优秀!