x, y = np.meshgrid(np.arange(range_x), np.arange(range_y)) # np.savetxt('reshape_data.txt', x, delimiter=' ', fmt="%i") x_o = x - range_x / 2 y_o = y - range_y / 2 x_i = x - dx y_i = y - dy c++ Eigen::Tensor
时间: 2024-01-17 16:04:42 浏览: 121
numpy.meshgrid()理解(小结)
5星 · 资源好评率100%
这段代码的C++ Eigen::Tensor实现如下:
```cpp
#include <iostream>
#include <Eigen/Dense>
#include <unsupported/Eigen/CXX11/Tensor>
using namespace Eigen;
using namespace std;
int main() {
int range_x = 100;
int range_y = 100;
Tensor<float, 2> x(range_x, range_y);
Tensor<float, 2> y(range_x, range_y);
for (int i = 0; i < range_x; i++) {
for (int j = 0; j < range_y; j++) {
x(i, j) = i;
y(i, j) = j;
}
}
Tensor<float, 2> x_o(range_x, range_y);
Tensor<float, 2> y_o(range_x, range_y);
Tensor<float, 2> x_i(range_x, range_y);
Tensor<float, 2> y_i(range_x, range_y);
float dx = 1.5;
float dy = 2.0;
for (int i = 0; i < range_x; i++) {
for (int j = 0; j < range_y; j++) {
x_o(i, j) = x(i, j) - range_x / 2.0;
y_o(i, j) = y(i, j) - range_y / 2.0;
x_i(i, j) = x(i, j) - dx;
y_i(i, j) = y(i, j) - dy;
}
}
cout << "x_o: \n" << x_o << endl;
cout << "y_o: \n" << y_o << endl;
cout << "x_i: \n" << x_i << endl;
cout << "y_i: \n" << y_i << endl;
return 0;
}
```
其中,使用了Eigen::Tensor来存储矩阵,通过双重循环来实现numpy中的arange()函数的功能,最后分别计算了x_o、y_o、x_i和y_i。注意,C++中整数相除默认向下取整,因此需要将除数改为2.0才能得到正确的结果。
阅读全文