It is very easy to delete the last rows or columns in a boost::multi_array
, for example, using the following minimal example:
#include "boost/multi_array.hpp"
#include <iostream>
typedef boost::multi_array<double, 2> array_type;
// Helper function for printing arrays
void print_array(const array_type& A){
for ( size_t i=0; i < A.shape()[0]; i++ ) {
for ( size_t j=0; j < A.shape()[1]; j++ )
std::cout << A[i][j] << " ";
std::cout << std::endl;
}
}
int main () {
// Create a 2D array that is 5 x 3
size_t dims[2] = {5, 3};
array_type A(boost::extents[dims[0]][dims[1]]);
// Assign values to the elements
int values = 0;
for(size_t i = 0; i < A.shape()[0]; ++i) {
for(size_t j = 0; j < A.shape()[1]; ++j) {
A[i][j] = values;
}
values++;
}
// Print the array
std::cout << "A:" << std::endl;
print_array(A);
// Delete rows and print the array again
A.resize(boost::extents[3][A.shape()[1]]);
std::cout << std::endl << "A after resizing:" << std::endl;
print_array(A);
return 0;
}
This code produces the following output:
A:
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
A after resizing:
0 0 0
1 1 1
2 2 2
As one can see, the resize
operation simply deleted the last rows of the array.
Question: Would it be possible to delete the first rows (or columns) instead, such that the output of the program would become
A:
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
A after resizing:
2 2 2
3 3 3
4 4 4
Thank you very much in advance for your help!