我可以删除boost::multi_array的单个维度而不复制其内容吗
Can I remove a single dimension of a boost::multi_array without copying its content?
下面是我的代码,用于将boost::multi_array从形状[3 4 2]
重塑为[12 2]
。由于维度在boost::multi_array中是固定的,所以我首先将三维数组重塑为[12 2 1]
形状,然后将前两个维度复制到一个新的数组中。
有没有一种更简单的方法不进行任何复制(我更喜欢按照numpy.reform的精神查看原始数据)。
使用g++ -g test.cc && ./a.out
运行
#include "boost/multi_array.hpp"
#include <cassert>
#include <iostream>
int
main () {
// Create a 3D array that is 3 x 4 x 2
typedef boost::multi_array<double, 3> array_type;
typedef array_type::index index;
array_type A(boost::extents[3][4][2]);
// indexer can be const a boost::array
boost::array<array_type::index,3> idx = {{0,0,0}};
// Assign values to the elements
int values = 0;
for(index i = 0; i != 3; ++i)
for(index j = 0; j != 4; ++j)
for(index k = 0; k != 2; ++k)
A[i][j][k] = values++;
std::cout << "array elements: " << A.num_elements() << std::endl;
std::cout << "array ndim: " << A.num_dimensions() << std::endl;
std::cout << "array size: " << A.size() << std::endl; // equivalent to a.shape()[0];
std::cout << "array shape: " << A.shape()[0] <<" " << A.shape()[1] <<" " << A.shape()[2] << std::endl;
int verify = 0;
for(index i = 0; i != 3; ++i)
for(index j = 0; j != 4; ++j)
for(index k = 0; k != 2; ++k) {
std::cout << "A[" <<i <<"]["<<j<<"]["<<k<<"] = " << A[i][j][k] << std::endl;
assert(A[i][j][k] == verify++);
}
boost::array<array_type::index,3> dims2 = {{12, 2, 1}};
A.reshape(dims2);
std::cout << "array shape: " << A.shape()[0] <<" " << A.shape()[1] <<" " << A.shape()[2] << std::endl;
for(index i = 0; i != 12; ++i)
for(index j = 0; j != 2; ++j)
for(index k = 0; k != 1; ++k) {
std::cout << "A[" <<i <<"]["<<j<<"]["<<k<<"] = " << A[i][j][k] << std::endl;
}
typedef boost::multi_array<double, 2> Array2d;
Array2d B(boost::extents[12][2]);
for(index i = 0; i != 12; ++i)
for(index j = 0; j != 2; ++j){
B[i][j] = A[i][j][0];
std::cout << "B[" <<i <<"]["<<j<<"] = " << B[i][j] << std::endl;
}
return 0;
}
我认为在一般情况下无法做到这一点。您可以生成一个维度较低的子视图。我读过你的代码,你想让你的二维数组有和原始三维数组一样多的元素。
您可以使用const_multi_array_ref
:重复使用3D阵列的现有数据
boost::multi_array< int, 3 > a( boost::extents[ 2 ][ 3 ][ 4 ] );
boost::const_multi_array_ref< int, 2 > b( a.data(), boost::extents[ 2 ][ 12 ] );
根据储存顺序,这可能会达到你想要的效果。
我建议在multi_array
周围编写一个小包装器,该包装器使用所需的索引计算来访问multi_array
。
事实证明,使用视图(参见创建视图)可以实现这一点。
在我的例子中:
... reshape ...
typedef boost::multi_array_types::index_range range;
array_type::index_gen indices;
array_type::array_view<2>::type myview =
A[ indices[range(0,12)][range(0,2)][0] ];
for(index i = 0; i != 12; ++i)
for(index j = 0; j != 2; ++j){
std::cout << "myview[" <<i <<"]["<<j<<"] = " << myview[i][j] << std::endl;
相关文章:
- 为什么我们将单个或多维数组的大小声明为常量值?
- QT QOpenGLWidget:如何在不使用数据块复制的情况下修改VBO中的单个顶点值?
- 如何将多维字符数组复制到字符 * 中
- 将指向多维数组向量中的单个项的指针传递给函数 c++
- std::将三维数组复制到三维向量中
- 坚持将双指针管理的内存复制到二维数组
- 有没有办法将两个输入图像堆叠到卤化物发生器中的单个 4 维缓冲区中?
- 将值复制到多维数组时出现问题
- 如何在不复制数据的情况下在平面数组和多维数组之间进行转换?
- 在Java中,是否可以编写单个函数或类来处理任意多维ArrayList?
- char 数组在上次迭代时复制整个单词而不是单个字母
- C++复制多维向量
- 复制二维阵列C++
- C 复制三维数组
- 标准::复制二维数组
- 如何从图像矢量复制单个图像到临时cv::Mat
- 我可以同时复制单个shared_ptr吗?
- 在不带指针的C-string中复制单个字符
- 最快的方式复制一维二维矢量在c++
- 使用std::copy复制单个字符