0

Lets say for example I have a 2 dimensional vector of integers, e.g:

0,1,4,7,3,7,3,7,7,2
3,6,3,1,7,9,2,8,3,6
1,4,9,3,5,8,3,2,8,5
2,5,8,4,9,3,2,9,0,1
3,6,3,1,7,9,2,8,3,6
1,4,9,3,5,8,3,2,8,5

and I want to get another 2D vector that contains the values from index 2 to 5 horizontally and indexes 1 to 3 vertically, I will illustrate the values I need with '■'.

0,1,4,7,3,7,3,7,7,2
3,6,■,■,■,■,■,8,3,6
1,4,■,■,■,■,■,2,8,5
2,5,■,■,■,■,■,9,0,1
3,6,3,1,7,9,2,8,3,6
1,4,9,3,5,8,3,2,8,5

the 2D vector I want in this instance:

3,1,7,9,2
9,3,5,8,3
8,4,9,3,2

I need this to be very efficient since I an using this in a convolutional neural network to get the area of the input image that the filter is currently over, and it will need to do this thousands of times. (in my case I will be using it to get a 3D vector out of another 3D vector, but I assume scaling it up shouldn't be too difficult, and it is much easier to explain my idea using 2D vectors)

  • 3
    Just do it, then if you have a performance problem on this part, you can start by showing us what you have done. premature optimization is the root of all evil. – Jaffa Mar 09 '20 at 14:36
  • 2
    The "very efficient" way is to implement a 2D vector abstraction that can be configured as a view of a subsection of another 2D vector without involving any kind of copying, similarly to what `std::string_view` does for `std::string`s. Unfortunately, like most non-trivial things in C++, this requires a pile of code to be written, and this won't be an on-topic answer for stackoverflow.com. I encourage you to continue reading your C++ book and practice the sample problems, until you gain sufficient skill and knowledge to be able to implement something like this. – Sam Varshavchik Mar 09 '20 at 14:36
  • 2
    FWIW, if you want performance, you do **not** want a 2d vector. you want to use a 1d vector and pretend it has multiple dimensions like: https://stackoverflow.com/a/60546369/4342498 – NathanOliver Mar 09 '20 at 14:44

1 Answers1

0

I've made a function for you to get a submatrix from a matrix

std::vector<std::vector<int>> subMatrix(std::vector<std::vector<int>>& matrix, 
    unsigned int x, unsigned int y, unsigned int width, unsigned int height) {
    std::vector<std::vector<int>> submatrix;
    for(int i = 0; i < width; i ++) {
        std::vector<int> vect;
        for(int j = 0; j < height; j ++)
            vect.push_back(matrix[i + x][j + y]);
        submatrix.push_back(vect);
    }
    return submatrix;
}

And also another function to show matrices:

void showMatrix(std::vector<std::vector<int>>& matrix) {
    for(std::vector<int> vect : matrix) {
        for(int n : vect) {
            std::cout << n << " ";
        }
        std::cout << "\n";
    }
}

This is how I'm using them:

int main(void) {

    std::vector<std::vector<int>> matrix = {
        {0,0,0,0},
        {0,1,0,0},
        {0,0,1,0},
        {0,0,0,0},
    };
    showMatrix(matrix);

    std::cout << "\n";
                                                           //  x, y, width, height
    std::vector<std::vector<int>> submatrix = subMatrix(matrix, 1, 1, 3, 3);
    showMatrix(submatrix);

    return 0;
}

The output:

0 0 0 0
0 1 0 0
0 0 1 0
0 0 0 0

1 0 0
0 1 0
0 0 0