简体   繁体   English

如何在Matlab / octave和C ++之间正确比较结果

[英]How to properly compare results between matlab/octave and C++

I'm writing some piece of code available in Matlab/Octave into C++ code. 我正在将Matlab / Octave中可用的一些代码编写为C ++代码。 I only have octave so I will just say octave from now on. 我只有八度,所以从现在开始我只说八度。 I want to properly compare the results between the octave code and the C++ code. 我想正确比较八度代码和C ++代码之间的结果。 The algorithms I'm writing take as input a 2D matrix, and output another 2D matrix. 我正在编写的算法将2D矩阵作为输入,然后输出另一个2D矩阵。

To compare the results, I write the input matrix A from octave using the function save A.mat A , with default options. 为了比较结果,我使用带有默认选项的功能save A.mat A从八度写输入矩阵A This creates an ascii file A.mat which starts like 这将创建一个ascii文件A.mat,其开始类似于

# Created by Octave 3.8.1, Tue May 27 12:12:53 2014 CEST <remi@desktop>
# name: values
# type: matrix
# rows: 25
# columns: 5 
43.0656 6.752420000000001 68.39323 35.75617 98.85446
...

I run the algorithm using octave and save the output matrix B similarly. 我使用八度运行算法,并类似地保存输出矩阵B

In my C++ code, I load the matrices A and B using the following piece of code: 在我的C ++代码中,我使用以下代码加载矩阵A和B:

// I opened the file A.mat with std::ifstream infile(filename);
// and read the first lines starting by # and loaded the matrix dimensions
std::string buffer;
double* matBuffer = new double[rows*cols];
while (std::getline(infile, buffer)) {
  std::istringstream iss(buffer);
  while (iss >> *matBuffer) {
    matBuffer++;
  }
}

Then I run the C++ code with the values read from A.mat, and compare the results with the values read from B.mat by computing the mean squared error(MSE) on the coeff of B read vs B computed. 然后,我使用从A.mat读取的值运行C ++代码,并通过计算B读取与B计算的系数的均方误差(MSE) ,将结果与从B.mat读取的值进行比较。

However, with such a design, can I expect that the MSE be 0 between the C++ and octave code? 但是,采用这种设计,我可以期望C ++和倍频程代码之间的MSE为0吗? Of course I do the computation on octave and C++ with the same machine. 当然,我使用同一台计算机在八度和C ++上进行计算。 But what about the loss in precision due to writing/reading the matrices in files? 但是,由于写入/读取文件矩阵会导致精度损失呢? Also, I assume that coefficients of octave matrices are stored in double by default, is this correct? 另外,我假设八度矩阵的系数默认情况下存储为double,这是正确的吗?

can I expect that the MSE be 0 between the C++ and octave code? 我可以期望C ++和八度代码之间的MSE为0吗?

I don't think so, because of the many levels of conversion, a precision loss is hard to avoid. 我不这么认为,由于转换级别很多,因此很难避免精度损失。

Also, I assume that coefficients of octave matrices are stored in double by default, is this correct? 另外,我假设八度矩阵的系数默认情况下存储为double,这是正确的吗?

Octave uses double precision for internal representation of the values, but again there can be a loss in precision when storing the values in ASCII. 八度使用精度来表示值的内部表示,但是当以ASCII格式存储值时,精度也会再次下降。

I'd suggest you try to use the binary format for storing the values, which will exclude any problems with precision. 我建议您尝试使用二进制格式存储值,这将排除任何精度问题。 You can go with the HDF5 format by using 您可以通过使用HDF5格式

save -hdf5 A.mat A

You can then use the HDF5 API to read the values in your CPP application. 然后,您可以使用HDF5 API读取CPP应用程序中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM