Matrix Problem

B MATRİX

a b c d
e f g h
i j k l

C MATRİX
w x
y z

A MATRİX

aw+bx+ey+fz , bw+cx+fy+gz , cw+dx+gy+hz

ew+fx+iy+jz , fw+gx+jy+kz , gw+hx+ky+lz




I could not
how do I write a code to create a matrix ?

Last edited on
how do I write a code to create a matrix


vector< vector<double> > M;
"to create"?

As in "container to store N values".
or
As in "to compute A = func( B, C )"?
Did you mean:

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 );
}


3.4	4.4	5.4	
7.4	8.4	9.4	
Last edited on

I mean, how can I write this code more simply
WHAT code more simply?

We haven't really got any idea what you are trying to do and you haven't provided any code to make more "simple".

excuse me you have a point there!
Topic archived. No new replies allowed.