小编Jou*_*ske的帖子

在Fortran中选择给定逻辑向量的数组的子集

在Fortran中,是否可以通过使用一些逻辑值向量而不是索引来选择数组的某些部分?例如这样:

iszero(1) = 0
iszero(2) = 1
iszero(3) = 0
sum0 = sum(iszero) !was  sum0 = sum(iszero==0)
!mymatrix is arbitary is 3 times 3 array
mysubmatrix(1:sum0,1:sum0) = mymatrix(iszero==0,iszero==0)
call dtrmv('l','n','u',sum0,mysubmatrix(1:sum0,1:sum0),sum0,x(1:sum0)),1)
Run Code Online (Sandbox Code Playgroud)

如果直接无法做到这一点,是否有一种简单(快速)的方法来找到iszero = 0的索引?

编辑:我改变了示例以呈现更现实的情况,在前一种情况下我只是将一些值更改为100.0d0,其中元素处理可以正常.

edit2:在代码的第四行有一个类型

arrays fortran logical-operators

3
推荐指数
2
解决办法
8369
查看次数

Rcpp:将C数组作为NumericMatrix返回到R

#include <Rcpp.h>
#include <vector>
extern "C"
{
  #include "cheader.h"
}

using namespace Rcpp;

// [[Rcpp::export]]

NumericVector cppfunction(NumericVector inputR){

  double const* input = inputR.begin();
  size_t N = inputR.size();
  double output[10*N];
  cfunction(input, N, output);
  std::vector<double> outputR(output, output + sizeof(output) / sizeof(double));
  return wrap(outputR);
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,除了我必须手动将矢量outputR转换为R中的矩阵.我当然也可以将outputR转换为NumericMatrix(或者我可以?)然后返回,但我真正的问题是上述过程是最优的吗?我是否必须先将输出转换为std :: vector,然后再转换为NumericVector/Matrix,还是可以以某种方式避免这种情况?我试着直接包装输出但是没有用.

c c++ r rcpp

3
推荐指数
1
解决办法
3545
查看次数

Fortran对R中的for-loop等仲裁索引进行了循环操作?

我有两个p倍-N阵列xmissx,其中x包含任意数目和missx是包含零和一的阵列.我需要对missx零点进行递归计算.显而易见的解决方案是这样的:

do i = 1, n
   do j = 1, p
      if(missx(j,i)==0) then
         z(j,i) = ... something depending on the previous computations and x(j,i)
      end if
   end do
end do
Run Code Online (Sandbox Code Playgroud)

这种方法的问题在于大部分时间missx总是为0,因此有很多if陈述总是如此.

在R中,我会这样做:

for(i in 1:n)
  for(j in which(xmiss[,i]==0))
     z[j,i] <- ... something depending on the previous computations and x[j,i]
Run Code Online (Sandbox Code Playgroud)

有没有办法在Fortran中进行内循环?我确实试过这样的版本:

do i = 1, n
   do j = 1, xlength(i) !xlength(i) gives the number of zero-elements …
Run Code Online (Sandbox Code Playgroud)

fortran loops if-statement r

2
推荐指数
1
解决办法
440
查看次数

访问一个月的第一个工作日

我需要知道某个月的第一个工作日,R中是否有一些包含相关功能的包?

r date time-series

1
推荐指数
3
解决办法
3637
查看次数

标签 统计

r ×3

fortran ×2

arrays ×1

c ×1

c++ ×1

date ×1

if-statement ×1

logical-operators ×1

loops ×1

rcpp ×1

time-series ×1