计算中误差的c++代码
时间: 2023-11-04 21:09:43 浏览: 55
计算中误差(mean absolute error,MAE)的 C 代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float mae(float* x, float* y, int n) {
float sum = 0.0;
for (int i = 0; i < n; i++) {
sum += fabs(x[i] - y[i]);
}
return sum / n;
}
int main() {
float x[] = {1.0, 2.0, 3.0, 4.0, 5.0};
float y[] = {1.5, 2.5, 3.5, 4.5, 5.5};
int n = 5;
float result = mae(x, y, n);
printf("MAE: %f\n", result);
return 0;
}
```
其中,`mae` 函数计算了输入数组 `x` 和 `y` 的 MAE 值,`n` 表示数组的长度。在 `main` 函数中,我们定义了两个数组 `x` 和 `y`,分别代表真实值和预测值,然后调用 `mae` 函数计算 MAE 值,并输出结果。
相关问题
立体匹配计算均方根误差c++代码
以下是一个简单的Python代码,用于计算立体匹配的均方根误差(RMSE):
```
import numpy as np
def stereo_matching_rmse(left_img, right_img, disparity):
# 计算差异图
diff = np.abs(left_img - np.roll(right_img, disparity))
# 计算均方根误差
rmse = np.sqrt(np.mean(diff**2))
return rmse
```
其中,left_img和right_img分别是左右图像的灰度图像,disparity是视差(即左右图像中对应像素的水平位移量),函数返回RMSE值。请注意,此代码假定左右图像已经对齐,即不需要进行额外的图像对齐操作。
螺距误差补偿C++代码
以下是一个简单的C++代码示例,用于计算螺距误差补偿:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double lead, pitch, error, compensation;
cout << "Enter lead (mm): ";
cin >> lead;
cout << "Enter pitch (mm): ";
cin >> pitch;
cout << "Enter error (mm): ";
cin >> error;
compensation = (error / (2 * M_PI)) * asin((2 * M_PI) / (pitch / lead));
cout << "Compensation (mm): " << compensation << endl;
return 0;
}
```
该代码要求用户输入螺距、导程和误差,并计算出螺距误差补偿值。计算公式如下:
Compensation = (Error / (2 * π)) * arcsin((2 * π) / (Pitch / Lead))
其中,Lead是螺距,Pitch是导程,Error是螺距误差,π是圆周率。arcsin()函数是反正弦函数,可以通过<cmath>头文件中的asin()函数实现。