std :: inner_product以计算向量和向量的向量的标准偏差

问题描述 投票:0回答:1

我正在尝试创建一个函数来计算标准偏差。我尝试对inner_product的op1和op2使用std::inner_product和lambda表达式来修改std::inner_product函数的操作。

不幸的是,在主循环中调用函数时出现编译器错误:

error C2664: cannot convert parameter 1 from "std::vector<float,std::allocator<float>>" in "std::vector<std::vector<float,std::allocator<float>>,std::allocator<std::vector<float,std::allocator<float>>>> &"

这是我的代码:

#include <numeric>
#include <cmath>

float stdfunc(std::vector<float> const & invector) {
   float mean = std::accumulate(invector.begin(), invector.end(), 0.0) / invector.size();

   float sq_sum = std::inner_product(invector.begin(), invector.end(), invector.begin(), 0.0,
   [](float const & x, float const & y) {return x+y;},
   [mean](float const & x, float const & y) {return (x-mean)*(y-mean);});

   return std::sqrt(sq_sum / (invector.size() - 1));
}

呼叫主:

int main(){
std::vector<float> testv {6,3,2,9,11,44,20};
float stdw = stdfunc(testv);
std::cout << "Standardabw: " << stdw << std::endl;
    return 0;
}

希望您能帮助我。

我还想知道如何修改代码,以便能够为每列计算矢量的二维矢量的标准偏差。

c++ compiler-errors numeric cmath
1个回答
0
投票

错误消息表示,参数为std::vector<float>的类型与功能参数为std::vector<std::vector<float>>的类型不对应。

© www.soinside.com 2019 - 2024. All rights reserved.