首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RcppParallel: RMatrix和RVector算术运算

RcppParallel: RMatrix和RVector算术运算
EN

Stack Overflow用户
提问于 2016-01-18 19:37:20
回答 1查看 481关注 0票数 1

我试图用RcppArmadillo并行一个双for循环,但是对于RMatrixRVector可用的算术操作,我遇到了困难。我查看了github上可用的头文件,没有看到任何东西,所以我想我找错地方了。这是我的工作人员,我评论了我试图在两个RMatrix对象之间执行算术操作的地方。

代码语言:javascript
复制
#include <RcppParallel.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <Rmath.h>
#include <RcppArmadillo.h>
using namespace RcppParallel;


struct ClosestMean : public Worker {

  // Input data and means matrix
  const RMatrix<double> input_data;
  const RMatrix<double> means;

  // Output labels
  RVector<int> predicted_labels;

  // constructor
  ClosestMean(const Rcpp::NumericMatrix input_data, const Rcpp::NumericMatrix means, Rcpp::IntegerVector predicted_labels)
    : input_data(input_data), means(means), predicted_labels(predicted_labels) {}

  // function call operator for the specified range (begin/end)
  void operator () (std::size_t begin, std::size_t end){
    for (unsigned int i = begin; i < end; i++){

      // Check for User Interrupts
      Rcpp::checkUserInterrupt();

      // Get the label corresponding to the cluster mean
      // for which the point is closest to
      RMatrix<double>::Row point = input_data.row(i);
      int label_min = -1;
      double dist;
      double min_dist = INFINITY;

      for (unsigned int j = 0; j < means.nrow(); j++){
        RMatrix<double>::Row mean = means.row(j);
        dist = sqrt(Rcpp::sum((mean - point)^2)); // This is where the operation is failing
        if (dist < min_dist){
          min_dist = dist;
          label_min = j;
        }
      }

      predicted_labels[i] = label_min;

    }
  }

};

谢谢你的建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-20 07:29:09

基本上,您不能像使用常规的Rcpp向量那样减去两个Row对象(即,利用所谓的Rcpp糖) --它只是没有为RcppParallel包装器实现。您必须自己编写迭代。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34862702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档