Answer the question
In order to leave comments, you need to log in
How to pad a two-dimensional matrix with zeros around the edges?
There is a matrix of size w0 h0
(stored in memory as a vector of integer vectors). It is necessary to add zeros to the left and right, and add w1
zeros to the bottom and top h1
.
w0 = 2
h0 = 3
w1 = 1
h1 = 2
7 8
3 4
9 1
0 0 0 0
0 0 0 0
0 7 8 0
0 3 4 0
0 9 1 0
0 0 0 0
0 0 0 0
Answer the question
In order to leave comments, you need to log in
First resize each line to size size()+w0+w1
. Then shift the elements w0 positions to the right (the loop must be driven from the end). Then fill in the empty spaces with zeros.
The same is true in the vector of vectors itself. Only instead of numbers it is necessary to shift the vectors by h0 positions. It will be necessary to write to empty places std::vector(n+w0+w1, 0);
Edit: In general, nothing needs to be added to the array here. You can finish the answer you need with zeros right during the output.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question