multiplication of matrices using vectors

i am trying to multiply 2 matrices (src) & (src_trans) by passing matrices(2d vectors) to the function.
the output is showing only 0's in the product of these two matrices
i cant find the error
i have checked the dimensions again and again but can't find the answer.
please help
thanks in advance:)


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 #include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include<conio.h>
#include <iostream>
#include <cmath>
#include <vector>
#define nxsrc 12
#define nysrc 28
using namespace std;

void print(vector<vector<int> > matrix, int r, int c)
{   for (int i=0; i<r; ++i) 
  {
  	for (int j=0;j<c;++j){
  		cout<<matrix[i][j];
	  cout<<" " ;}
	  cout<<endl;
}
}

vector<vector<int> > transpose(vector<vector<int> > matrix, int r, int c)
{
	vector<vector< int> > outtrans(nysrc);
    for ( int i=0;i<c;i++){
          outtrans[i].resize(r);
    }
	
 for (int i=0;i<r;++i){
        for (int  j=0;j<c;++j)
            outtrans[j][i] = matrix[i][j];
   }
   return outtrans;	
}

vector<vector<int> > multiply(vector<vector<int> > m1,vector<vector<int> > m2,int a,int b,int e,int f)
{
	vector<vector<int> > d(a);
    for ( unsigned int i=0;i<f;i++){
          d[i].resize(a);
}   
      for (unsigned int i=0;i<a;i++) {
      	vector<int> myRow;
      	 myRow.reserve(a);
//      	 myRow.push_back({});
//      	  d.push_back(myRow);
        for (unsigned int j=0;j<f;j++) {
				int sum=0;
              for (unsigned int k=0;k<f;k++) {	
            sum+=m1[i][k]*m2[k][j];
            myRow.push_back(sum);
//		    d.push_back(myRow);
			}
		    d.push_back(myRow);
        }
    }
    return d;
}  

int main()
{
  	int src[nxsrc][nysrc];	  
	int count;
    for (int i=0 ; i<nxsrc ; i++ ){
	  for( int j=0; j<nysrc ; j++){
		 src[i][j] = 2;
		 }     
	}
	
    vector<vector< int> > src_vec(nxsrc);
	for ( int i = 0 ; i < nxsrc ; i++ ){
	   src_vec[i].resize(nysrc);
	}

    for (int m = 0; m < nxsrc; m++){     
        for (int n = 0; n < nysrc; n++){
            src_vec[m][n] = src[m][n];
        }
    }  
    
	getch();
	cout<<"src matrix"<<endl;
    print(src_vec,nxsrc,nysrc); /*initializing src_trans with zeros*/
    vector<vector< int> > src_trans(nysrc);
    for ( int i = 0 ; i < nysrc ; i++ ){
          src_trans[i].resize(nxsrc);
    }
    
    cout<<"rows of src"<<src_vec.size()<<endl;
    cout<<"cols of src"<<src_vec[0].size()<<endl;
    cout<<endl;
	getch();
	src_trans=transpose(src_vec,nxsrc,nysrc);
	
	cout<<"transpose of src"<<endl;
    print(src_trans,nysrc,nxsrc);
 cout<<endl;
    vector<vector< int> > src_src_trans(nxsrc);
     for ( int i = 0 ; i < nxsrc ; i++ ){
          src_src_trans[i].resize(nxsrc);
     }
         print(src_src_trans,nxsrc,nxsrc);         
         cout<<endl;
 
getch();
int r_src_trans=src_src_trans.size();
int c_src_trans=src_src_trans[0].size();


     src_src_trans =  multiply(src_trans,src_vec,nxsrc,nysrc,r_src_trans,c_src_trans);
    getch();
    cout<<endl<<"product of src & src_trans"<<endl;
    print(src_src_trans,nxsrc,nxsrc);
    cout<<endl
Last edited on
Hints:
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
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>

constexpr unsigned ROWS { 12 };
constexpr unsigned COLS { 28 };

using matrix = std::vector<std::vector<int>>;

void print(const matrix& m);
matrix transpose(const matrix& m);
matrix multiply(const matrix& m1, const matrix& m2);

int main()
{
    matrix src(ROWS, std::vector<int>(COLS, 2));

    std::cout << "src matrix:\n";
    print(src);

    std::cout << "rows of src: " << src.size()
              << "\ncols of src: " << src.at(0).size() << '\n';

    matrix src_trans(COLS, std::vector<int>(ROWS));

    src_trans = transpose(src);

    std::cout << "Transpose of src:\n";
    print(src_trans);

    // mul rows == src rows
    // mul columns == src_trans columns
    matrix mul(src.size(), std::vector<int>(src_trans.at(0).size()));

    mul = multiply(src, src_trans);
    std::cout << "\nProduct of src * src_trans:\n";
    print(mul);
}

void print(const matrix& m)
{
    for(const auto& v : m) {
        for(const int i : v) { std::cout << i << ' '; }
        std::cout << '\n';
    }
    std::cout << '\n';
}

matrix transpose(const matrix& m)
{
    matrix outtrans(m.at(0).size(), std::vector<int>(m.size()));
    for (std::size_t i = 0; i < m.size(); ++i) {
        for (std::size_t j = 0; j < m.at(0).size(); ++j) {
            outtrans.at(j).at(i) = m.at(i).at(j);
        }
    }
    return outtrans;
}

matrix multiply(const matrix& m1, const matrix& m2)
{
    matrix result(m1.size(), std::vector<int>(m2.at(0).size()));

    for(std::size_t row = 0; row < result.size(); ++row) {
        for(std::size_t col = 0; col < result.at(0).size(); ++col) {
            for(std::size_t inner = 0; inner < m2.size(); ++inner) {
                result.at(row).at(col) += m1.at(row).at(inner) * m2.at(inner).at(col);
            }
        }
    }
    return result;
}


This could interest you:
http://en.cppreference.com/w/cpp/numeric/valarray
Topic archived. No new replies allowed.