请考虑以下代码段:
>k<-5
>T<-t(combn(k+1,k))
>T
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 1 2 3 4 6
[3,] 1 2 3 5 6
[4,] 1 2 4 5 6
[5,] 1 3 4 5 6
[6,] 2 3 4 5 6
Run Code Online (Sandbox Code Playgroud)
除了第一线,每条线
T[i,]
的T
具有k
元件,k-1
其中与常见的T[i-1,]
.
我想重新排序给定行中的条目,以便T[i,], i>1
每行上的"新"条目放在最后一列中.
即我希望重新排序的表看起来像:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 1 2 3 4 6
[3,] 1 …
Run Code Online (Sandbox Code Playgroud) 我需要找到k
两个排序数组中的最大元素,但有一个扭曲.
该算法假定k<=max(m,n)
和索引出错时k>max(m,n)
.在我的问题我知道,总是会k>(m+n)/2
,因此k>min(m,n)
,所以我需要改变儒勒Olléon的回答有点......我只是不明白这一点:〜
我已经找到了这个链接第3页,但是那里有bug(当实现时,它没有返回正确的答案)
我知道快速解决方法是将两个数组乘以-1并取该联合中的k个最小值并将该答案乘以-1但这将使代码不可读.
这不是作业.
好吧,我想我很想念尼尔的回答或其他什么,因为这就是我给'他'的东西
#include <algorithm>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <Eigen/Dense>
using namespace Eigen;
using Eigen::VectorXf;
using Eigen::VectorXi;
float getNth(VectorXf& v1,VectorXf& v2,int& n){
int step=(n/4),i1=(n/2),i2=(n-i1);
while(!(v2(i2)>=v1(i1-1) && v1(i1)>v2(i2-1))){
if(v1(i1-1)>=v2(i2-1)){
i1-=step;
i2+=step;
} else {
i1+=step;
i2-=step;
}
step/=2;
if(!step) step=1;
}
if(v1(i1-1)>=v2(i2-1))
return v1(i1-1);
else
return v2(i2-1);
}
int main(){
int p,q,n,k,l;
float sol; …
Run Code Online (Sandbox Code Playgroud) 我有这个R包(MRwarping_1.0.zip).它安装在同事的32位Windows PC上,但无法安装在我的64位ubuntu上(并给出了一个非常奇怪的错误信息).我想知道是否
我通常在R
. 在两次模拟之间,R 代码的某些部分会发生变化。通常,我会在模拟结果旁边保存一个 .txt 文件,其中包含该模拟中使用的每个函数的定义。要制作该 .txt 文件,我只需运行以下行:
for(j in 1:length(ls())) print(c(ls()[j],eval(as.symbol(ls()[j]))))
out<-capture.output(for(j in 1:length(ls())) print(c(ls()[j],eval(as.symbol(ls()[j])))))
cat(out,file=paste("essay_4_code.txt",sep=""),sep="\n",append=FALSE)
Run Code Online (Sandbox Code Playgroud)
在我的环境中加载所有功能之后。然而,在生成的文本文件中,R 函数不是 R 可以解释为函数的格式。为了理解原因,这里有一个简单的例子:
rm(list=ls())
foo1<-function(x){
sin(x)+3
}
foo2<-function(x){
cos(x)+1
}
foo3<-function(x){
cos(x)+sin(x)
}
Run Code Online (Sandbox Code Playgroud)
会产生:
[[1]]
[1] "foo1"
[[2]]
function (x)
{
sin(x) + 3
}
[[1]]
[1] "foo2"
[[2]]
function (x)
{
cos(x) + 1
}
[[1]]
[1] "foo3"
[[2]]
function (x)
{
cos(x) + sin(x)
}
Run Code Online (Sandbox Code Playgroud)
所以,简而言之,我想让essay_4_code.txt R 可读
我正在使用R中的RNetCDF软件包.我正在尝试从导入的.nc数据集中获取所有变量名称的列表(我可以从我知道名字的少数变量中轻松获取值的向量,但我只知道所有变量名称的一小部分).
所以假设
float bar [5] = { 5.7106, 3.1768, 5.7458, 4.5859, 2.2718 };
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个功能,如:
cummin(bar,bar+bar.size());
Run Code Online (Sandbox Code Playgroud)
这将返回bar的条目的就地累积最小值,例如,该条包含:
5.7106, 3.1768, 3.1768, 3.1768, 2.2718
Run Code Online (Sandbox Code Playgroud)
我的问题是,还有可能比天真的方法更有效地实现累积分钟(它在我正在研究的代码中用于瓶颈函数,我认为它不能被矢量化).
(在ubuntu下),我尝试:
install.packages("png")
Run Code Online (Sandbox Code Playgroud)
得到:
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/home/kaveh/R/x86_64-pc-linux-gnu-library/3.2/png/libs/png.so':
libpng16.so.16: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我认为它必须与libpng
obselete有关,但我已经使用以下链接从这个链接安装了它:
./configure
make check
make install
Run Code Online (Sandbox Code Playgroud)
所以我真的不知道还能做些什么〜
不久之前,用户ggael回答了将eigen :: vectorXf映射到eigen :: matrixXf的问题.
现在,我需要做类似的事情,但是对于现有的矩阵,例如我知道我可以:
for(int i=0;i<p;++i){
VectorXf vec=q.col(i);
/*q is a p**2 by n matrix*/
Map<MatrixXf> qi(vec.data(),p,p);
/*run function that uses qi to produce a scalare and store that scalar*/
}
Run Code Online (Sandbox Code Playgroud)
但是(在我看来)qi
在循环外创建一劳永逸,然后qi
一遍又一遍地使用相同的东西会更有效(是吗?)
另外,我想知道我映射q.col(i)
到的中间步骤vec
是否真的有必要......
最近的答案建议:
qi=Map<MatrixXd>(vec.data(),p,p);
Run Code Online (Sandbox Code Playgroud)
但这样做会产生:
In function ‘Eigen::VectorXi DepType(const MatrixXf&, const MatrixXf&, const int&)’:
DeC.cpp:279:34: error: no matching function for call to ‘Eigen::Map<Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001> >::Map(Eigen::PlainObjectBase<Eigen::Matrix<float, -0x00000000000000001, 1> >::Scalar*, int&, int&)’
DeC.cpp:279:34: note: candidates are: …
Run Code Online (Sandbox Code Playgroud) 我正在尝试重现lm.fit功能正在做什么.但我有一个问题,它调用'C_Cdqrls'例程:
n<-100
p<-5
x<-cbind(1,matrix(rnorm(n*(p-1)),n,p-1))
res<-rnorm(n)
y<-x%*%bet+res
tol<-1e-07
z<-.Call(C_Cdqrls, x, y,tol)
Run Code Online (Sandbox Code Playgroud)
我得到:
Error: object 'C_Cdqrls' not found
Run Code Online (Sandbox Code Playgroud)
使用'C_Cdqrls'代替产生:
the Error in .Call("C_Cdqrls", x, y, tol) :
C symbol name "C_Cdqrls" not in load table
Run Code Online (Sandbox Code Playgroud)
同样,summary.lm()在lm-object上调用一个名为qr.lm的函数,但是:
qr.lm(lm(y~x-1))
Run Code Online (Sandbox Code Playgroud)
收益率:
Error: could not find function "qr.lm"
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何同时获取.Call(C_Cdqrls,x,y,tol)和qr.lm()以分别给出与lm.fit()和summary.lm()调用时相同的结果?
因此,当我将以下x次复制粘贴到python提示符时,它将日志x次添加到指定文件的末尾。
如何更改代码,以便每次将其复制粘贴到提示符时,我都只会覆盖现有文件(代码似乎不接受该
mode = 'w'
选项,或者我似乎不理解其含义)
def MinimalLogginf():
import logging
import os
paths = {'work': ''}
logger = logging.getLogger('oneDayFileLoader')
LogHandler = logging.FileHandler(os.path.join(paths["work"] , "oneDayFileLoader.log"), mode='w')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
LogHandler.setFormatter(formatter)
logger.addHandler(LogHandler)
logger.setLevel(logging.DEBUG)
#Let's say this is an error:
if(1 == 1):
logger.error('overwrite')
Run Code Online (Sandbox Code Playgroud)
所以我运行了一次:MinmalLoggingf()
现在,我希望新的日志文件覆盖上一次运行时创建的日志文件:
MinmalLoggingf()
Run Code Online (Sandbox Code Playgroud)