Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ matlab模转换为c++;_C++_Matlab - Fatal编程技术网

C++ matlab模转换为c++;

C++ matlab模转换为c++;,c++,matlab,C++,Matlab,这是cornerX [0] 1041.0374994748950 double [1] 2489.4116123346407 double [2] 1029.5005409900616 double [3] 2477.8746538498076 double 角落 [0] 834.69193966025080 double [1] 852.22774706647908 double [2] 1787.5897232339489

这是cornerX

    [0] 1041.0374994748950  double
    [1] 2489.4116123346407  double
    [2] 1029.5005409900616  double
    [3] 2477.8746538498076  double
角落

    [0] 834.69193966025080  double
    [1] 852.22774706647908  double
    [2] 1787.5897232339489  double
    [3] 1805.1255306401772  double
这里是原始的matlab函数

cornerX=cornerX+(1-mod(cornerX,2));
cornerY=cornerY+(1-mod(cornerY,2));
void AutomaticMacbethDetection::CalculateBatchCenters(std::vector<double> cornerX, std::vector<double> cornerY)
{

    cornerX=cornerX+(1-mod(cornerX,2));
    cornerY=cornerY+(1-mod(cornerY,2));*/
    for (int i = 0; i < cornerX.size(); i++)
    {
        cornerX[i] = cornerX[i] + (1 - (Utilities::realmod(cornerX[i],2.0)));
    }
这是我的功能

cornerX=cornerX+(1-mod(cornerX,2));
cornerY=cornerY+(1-mod(cornerY,2));
void AutomaticMacbethDetection::CalculateBatchCenters(std::vector<double> cornerX, std::vector<double> cornerY)
{

    cornerX=cornerX+(1-mod(cornerX,2));
    cornerY=cornerY+(1-mod(cornerY,2));*/
    for (int i = 0; i < cornerX.size(); i++)
    {
        cornerX[i] = cornerX[i] + (1 - (Utilities::realmod(cornerX[i],2.0)));
    }
我已经根据Matlab的描述创建了我的函数

MOD(x,y) is x - n.*y where n = floor(x./y) if y ~= 0.  If y is not an
%   integer and the quotient x./y is within roundoff error of an integer,
%   then n is that integer.  The inputs x and y must be real arrays of the
%   same size, or real scalars.
你能解释一下为什么我会得到完全不同的结果吗?
我猜我使用的数组有点错误…

我假设区别在于你是通过值传递向量,而不是通过引用

简单比较:

倍频程/MATLAB

C++

#包括
#包括
#包括
#包括
双实模(双x,双y);
无效测试(标准::矢量和角点测试);
int main()
{
向量X={1041.0374994748950,
2489.4116123346407,
1029.5005409900616,
2477.8746538498076};
向量Y={834.69193966025080,
852.22774706647908  ,
1787.5897232339489  ,
1805.1255306401772 };
试验(X);
试验(Y);
std::copy(X.begin(),X.end(),std::ostream_迭代器(std::cout,“\t”);

标准::cout@Gilad,关于什么数据?@Atomic_alarm我已经添加了数据我已经测试了你的代码,不,这不是问题。请看我的input@Gilad,更改了测试数据,结果是一样的。请看链接。
#include <iostream>
#include <vector>
#include <cmath>
#include <iterator>

double realmod(double x, double y);
void test(std::vector<double> &cornerX);
int main()
{
    std::vector<double> X = {1041.0374994748950,
    2489.4116123346407,
    1029.5005409900616,
    2477.8746538498076};

    std::vector<double> Y = {834.69193966025080 , 
    852.22774706647908  ,
    1787.5897232339489  ,
    1805.1255306401772 };
    test(X);
    test(Y);
    std::copy(X.begin(),X.end(),std::ostream_iterator<double>(std::cout,"\t"));
    std::cout<<std::endl;
    std::copy(Y.begin(),Y.end(),std::ostream_iterator<double>(std::cout,"\t"));
    return 0;
}


double realmod(double x, double y)
{
    if (y == 0 )
        return x;
    else if ( x == y)
        return 0.0;
    else
        return x - floor(x/y) *y;
}

void test(std::vector<double> &cornerX)
{
    for (size_t i = 0; i < cornerX.size(); i++)
        cornerX[i] = cornerX[i] + (1 - realmod(cornerX[i],2.0));
}