I am working on a program where I need to use 2d Boost.MultiArray. I managed to initialize it and fill it with data. But I don't understand how to take subarray of size i,j if multiarray is of size m,n. Where i<=m and j<=n. Can anyone help me?
Code:
matrix_type matrix(boost::extents[width][height]);
read_matrix_from_file(file_content, matrix);
for (int rank = 1; rank < workers; rank++) {
auto subarray_size = (rest > 0) ? lines_per_worker + 1 : lines_per_worker;
rest--;
typedef boost::multi_array_types::index_range range;
size_t finish_line = subarray_size + bias - 1;
finish_line = (finish_line==bias)? finish_line+1:finish_line;
matrix_type::array_view<2>::type
current_process_batch = matrix[boost::indices[range(bias, subarray_size + bias - 1)][range(0, width)]];
}
Documentation https://www.boost.org/doc/libs/1_73_0/libs/multi_array/doc/user.html#sec_generators
Sample:
Live On Coliru
Prints
UPDATE
Re. Comment:
if you want to avoid the degenerate dimensions, here's how:
Live On Coliru
Prints
Basically, don't use literals
[i][j]but[range(i,i+1)][range(j,j+1)]