1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <vector>
using namespace std;
using matrix = vector< vector<double> >;
//======================================================================
matrix weighted( const matrix &base, const matrix &mask )
{
int brows = base.size(), bcols = base[0].size();
int mrows = mask.size(), mcols = mask[0].size();
int rows = brows - mrows + 1;
int cols = bcols - mcols + 1;
matrix result( rows, vector<double>( cols, 0 ) );
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
{
for ( int ii = 0; ii < mrows; ii++ )
{
for ( int jj = 0; jj < mcols; jj++ ) result[i][j] += base[i+ii][j+jj] * mask[ii][jj];
}
}
}
return result;
}
//======================================================================
void print( const matrix &M )
{
for ( auto &row : M )
{
for ( auto e : row ) cout << e << '\t';
cout << '\n';
}
}
//======================================================================
int main()
{
matrix B = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
matrix C = { { 0.2, 0.3 },
{ 0.4, 0.1 } };
matrix A = weighted( B, C );
print( A );
}
|