# Matrices multiplying by using vector.

• (2 Pages)
• 1
• 2

## 15 Replies - 13803 Views - Last Post: 01 May 2012 - 12:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=277520&amp;s=2535d5558b93e63871288d95b042eab7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 8
• Joined: 30-April 12

# Matrices multiplying by using vector.

Posted 30 April 2012 - 04:21 PM

So I am trying to create a matrices program
that will multiply together.

but I am getting an error on my MultiplyingMatrix function
C++\matricies\main.cpp|19|error: too few arguments to function 'double MultiplyingMatrix(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >)'|

And How would I multiply my hard-coded MatrixA and B(I know I have to use for loops but it seems like I cant find columes..?)?

```#include <iostream>
#include <vector>

using namespace std;

void DisplayMatrix(vector <vector<double> > x)
{
for(int row = 0; row < x.size(); row++)
{
for(int col = 0; col < x[row].size(); col++)
{
cout << x[row][col] << " ";
}
cout << endl;
}
cout << endl;
}

double MultiplyingMatrix(vector <vector<double> >& x1, vector <vector<double> >& x2)
{

double answer = new double [x1.size()][x2.size()]

}

int main()
{
////////////////////////// MatrixA
vector <vector<double> > MatrixA;

vector <double> rowA;
for(int i=0; i<3; i++)
rowA.push_back(0.0);

for(int i=0; i<2;i++)
MatrixA.push_back(rowA);

MatrixA[0][0] = 1;
MatrixA[0][1] = 2;
MatrixA[0][2] = 3;

MatrixA[1][0] = 4;
MatrixA[1][1] = 5;
MatrixA[1][2] = 6;

/////////////////////////// MatrixB
vector <vector<double> > MatrixB;
vector <double> rowB;

for(int i=0; i<4; i++)
rowB.push_back(0.0);

for(int i=0; i<3;i++)
MatrixB.push_back(rowB);

MatrixB[0][0] = 1;
MatrixB[0][1] = 2;
MatrixB[0][2] = 3;
MatrixB[0][3] = 4;

MatrixB[1][0] = 5;
MatrixB[1][1] = 6;
MatrixB[1][2] = 7;
MatrixB[1][3] = 8;

MatrixB[2][0] = 9;
MatrixB[2][1] = 10;
MatrixB[2][2] = 11;
MatrixB[2][3] = 12;

///////////////////////////// MatrixC
vector <vector<double> > MatrixC;

vector <double> rowC;

for(int i=0; i<4; i++)
rowC.push_back(0.0);
for(int i=0; i<2;i++)
MatrixC.push_back(rowC);

DisplayMatrix(MatrixA);
DisplayMatrix(MatrixB);
DisplayMatrix(MatrixC);

MultiplyingMatrix(MatrixA);
MultiplyingMatrix(MatrixB);

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Matrices multiplying by using vector.

### #2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 05:54 PM

The error message is easy. MultiplyingMatrix takes two arguments: the two matrices that you want to multiply. You can't call it the way you're doing on lines 89 and 90. You have to send both vectors at once:
```MultiplyingMatrix(MatrixA, MatrixB);
```

When you multiply two matrices, the result should be a matrix. So MultiplyingMatrix should return a vector<vector<double> >, not a double.

And what do you mean, you "can't find columns"? The length of each row in MatrixA is the number of columns in MatrixA. The length of each row of MatrixB is the number of columns for MatrixB, and also for the answer matrix.

This post has been edited by r.stiltskin: 30 April 2012 - 05:55 PM

Reputation: 0
• Posts: 8
• Joined: 30-April 12

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 06:47 PM

r.stiltskin, on 30 April 2012 - 05:54 PM, said:

The error message is easy. MultiplyingMatrix takes two arguments: the two matrices that you want to multiply. You can't call it the way you're doing on lines 89 and 90. You have to send both vectors at once:
```MultiplyingMatrix(MatrixA, MatrixB);
```

When you multiply two matrices, the result should be a matrix. So MultiplyingMatrix should return a vector<vector<double> >, not a double.

And what do you mean, you "can't find columns"? The length of each row in MatrixA is the number of columns in MatrixA. The length of each row of MatrixB is the number of columns for MatrixB, and also for the answer matrix.

I created my row as
```MatrixB.push_back(rowB);
```

So when I am doing for loops for multiplying vectors row(A)/column(,should I use
```MatrixB.push_back(rowB);
```
or, x2 as a column? Sorry I am not understanding C++ very well(I should have commented out) and a beginner with C++.

Woops, let me fix that,
I created my column* as
```MatrixB.push_back(rowB);
```

### #4 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 06:59 PM

You're sending a vector<vector<double> > to MultiplyingMatrix, and inside that function the vector's name is 'x2'. Now, x2 has "some" elements; each of those elements is a vector of doubles. Each of them represents a row of a matrix. Those rows are designated as x2[0], x2[1], ..., x2[m-1] where m represents the number of rows.

The first row, x2[0], has "some" elements; each of those elements is located in a column of the matrix. The first element, x2[0][0], is in the first column. The second element, x2[0][1], is in the second column. And so on.

So if x2 has just 3 rows and 3 columns, the entire 2nd column is:
x2[0][1], x2[1][1], x2[2][1].

Does that help?

### #5 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 07:12 PM

My suggestion for multiplying is that you don't use push_back at all to create each row in MultiplyingMatrix. It will be much easier if you first create a vector<vector<double> > matrix of the correct size filled with 0s, and then you can use ordinary matrix notation, x2[i][j], to access the elements.

Look at the second form of constructor shown on this vector reference page. It shows you how to create a vector of a specific size filled with "n" copies of a specific value.

Do you know how to determine the size of the "answer" matrix when you multiply two matrices?

Edit: sorry, that was partially wrong. Use the "explicit vector" constructor mentioned above to create one row of 0s of the required size. Then use push_back to push that vector<double> into the vector<vector<double> > to create the full matrix. You only have to create 1 row. Then you can push_back that 1 row as many times as you want in a loop. That will automatically create multiple copies of the row.

This post has been edited by r.stiltskin: 30 April 2012 - 07:25 PM

Reputation: 0
• Posts: 8
• Joined: 30-April 12

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 07:26 PM

r.stiltskin, on 30 April 2012 - 06:59 PM, said:

You're sending a vector<vector<double> > to MultiplyingMatrix, and inside that function the vector's name is 'x2'. Now, x2 has "some" elements; each of those elements is a vector of doubles. Each of them represents a row of a matrix. Those rows are designated as x2[0], x2[1], ..., x2[m-1] where m represents the number of rows.

The first row, x2[0], has "some" elements; each of those elements is located in a column of the matrix. The first element, x2[0][0], is in the first column. The second element, x2[0][1], is in the second column. And so on.

So if x2 has just 3 rows and 3 columns, the entire 2nd column is:
x2[0][1], x2[1][1], x2[2][1].

Does that help?

Don't I have to use rows from MatrixA since I am Multiplying rows*cols?
Matrix A is 2x3.
Matrix B is 3x4.
Matrix C should be 2x4.
i am getting overwhelmed by vectors.. *sigh*

Maybe I am getting overwhl

Reputation: 0
• Posts: 8
• Joined: 30-April 12

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 07:31 PM

r.stiltskin, on 30 April 2012 - 07:12 PM, said:

My suggestion for multiplying is that you don't use push_back at all to create each row in MultiplyingMatrix. It will be much easier if you first create a vector<vector<double> > matrix of the correct size filled with 0s, and then you can use ordinary matrix notation, x2[i][j], to access the elements.

Look at the second form of constructor shown on this vector reference page. It shows you how to create a vector of a specific size filled with "n" copies of a specific value.

Do you know how to determine the size of the "answer" matrix when you multiply two matrices?

Edit: sorry, that was partially wrong. Use the "explicit vector" constructor mentioned above to create one row of 0s of the required size. Then use push_back to push that vector<double> into the vector<vector<double> > to create the full matrix. You only have to create 1 row. Then you can push_back that 1 row as many times as you want in a loop. That will automatically create multiple copies of the row.

I am on the part where trying to figure out returning "answer" matrix.. Do i have to return it on each of the parameter?

So do I even need MatrixC? I mean if you are suggesting using the "explicit vector" I don't think it's necessary to have one.
```vector <vector<double> > MatrixC;

vector <double> rowC;

for(int i=0; i<4; i++)
rowC.push_back(0.0);
for(int i=0; i<2;i++)
MatrixC.push_back(rowC);
```

### #8 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 07:34 PM

deathparado, on 30 April 2012 - 10:26 PM, said:

Don't I have to use rows from MatrixA since I am Multiplying rows*cols?
Matrix A is 2x3.
Matrix B is 3x4.
Matrix C should be 2x4.

Yes, that's exactly correct. I meant don't use push_back during the actual process of multiplying. First create an "empty" (all zeroes) matrix as described above, and then during the multiplying (and adding) of the individual elements, you can use ordinary 2D array notation because that will be more convenient.

### #9 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 07:40 PM

deathparado, on 30 April 2012 - 10:31 PM, said:

I am on the part where trying to figure out returning "answer" matrix.. Do i have to return it on each of the parameter?

I don't understand the question.

Quote

So do I even need MatrixC? I mean if you are suggesting using the "explicit vector" I don't think it's necessary to have one.
```vector <vector<double> > MatrixC;

vector <double> rowC;

for(int i=0; i<4; i++)
rowC.push_back(0.0);
for(int i=0; i<2;i++)
MatrixC.push_back(rowC);
```

That's OK. I was just suggesting another way.
```vector<double> rowC( 4, 0.0 );
vector <vector<double> > MatrixC;
for(int i=0; i<2;i++)
MatrixC.push_back(rowC);
```

Reputation: 0
• Posts: 8
• Joined: 30-April 12

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 08:10 PM

```double MultiplyingMatrix(vector <vector<double> >& x1, vector <vector<double> >& x2)
{
for(int row = 0; row < x1.size(); row++)
{
for(int col = 0; col < x2[row].size(); col++)
{
}
cout << endl;
}
}

```

Not even sure I am going in right direction..

### #11 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 08:18 PM

deathparado, on 30 April 2012 - 10:31 PM, said:

I am on the part where trying to figure out returning "answer" matrix.. Do i have to return it on each of the parameter?

I think I just realized what you were asking there.

If I understand you correctly, the answer is that you shouldn't "return it" on either parameter. That is, you shouldn't modify either parameter, because you don't want to modify the original vectors in the calling function. You should return the answer -- MatrixC -- as the return value of the function. The function signature should be
```vector<vector double> > MultiplyingMatrix(const vector<vector<double> >& x1, const vector<vector<double> >& x2)
```

and it should literally
```return MatrixC;
```

deathparado, on 30 April 2012 - 11:10 PM, said:

```double MultiplyingMatrix(vector <vector<double> >& x1, vector <vector<double> >& x2)
{
for(int row = 0; row < x1.size(); row++)
{
for(int col = 0; col < x2[row].size(); col++)
{
}
cout << endl;
}
}

```

Not even sure I am going in right direction..

No. Multiplying matrices is a bit more complicated than you think. You can't just do it all in 1 step. You have to deal with them element-by-element.

Here's a pretty straightforward explanation: how to multiply matrices.

Notice that the return type should be vector<vector<double> >, not double. And I added const to the parameters to make it clear that they will not be altered by the function.

Reputation: 0
• Posts: 8
• Joined: 30-April 12

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 08:36 PM

I know how to multiply matrices on paper..

Well for mine:

MatrixA
(1 2 3)
(4 5 6)
MatrixB
(1 2 3 4)
(5 6 7 8)
(9 10 11 12)

matrixC(should be)
(38 44 50 56)
(83 98 113 128)

Well this is alternative for the multiplying part(nested for loops)
```vector<vector<double> > MultiplyingMatrix(const vector<vector<double> >& x1, const vector<vector<double> >& x2)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
x1[i][j]=0; // getting an error.
for(int k=0;k<2;k++)
{
x1[i][j]= x1[i][j]+(x1[i][k]*[k][j]);
}
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout << [i][j]; //MatricX multiplying
}
cout << endl;
}

return MatrixC;
}

```

I think I am going into right direction but I still don't understand why I have to use
```vector<vector<double> >
```
to return MatrixC.

### #13 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Matrices multiplying by using vector.

Posted 30 April 2012 - 08:50 PM

Because if you modify x1 in the function, it will modify MatrixA in main. So you should create MatrixC, or x3, or whatever you want to call it, as described in Post #9, in the MultiplyingMatrix function, and then return that "answer" matrix as the return value of the function.

And in main, you will assign the return value of the function to a matrix, as in:
```//in main
vector<vector<double> > MatrixC = MultiplyingMatrix(MatrixA, MatrixB);

```

That way, MatrixA and MatrixB will be undisturbed, and you will have a third matrix, MatrixC, containing the answer.

Reputation: 0
• Posts: 8
• Joined: 30-April 12

## Re: Matrices multiplying by using vector.

Posted 01 May 2012 - 11:02 AM

Well..
```vector<vector<double> > MultiplyingMatrix(const vector<vector<double> >& A, const vector<vector<double> >& B, vector< vector<double> > &C)
{
int vrows;
int vcols;
vrows = A.size();
vcols = A[1].size();

for (int i = 0; i < vrows; i++)
{
for (int j = 0; j < vcols; j++)
{
for (int rwcl = 0; rwcl < vcols; rwcl++)
{
C[i][j] += (A[i][rwcl] * B[rwcl][j]);
}
cout << C[i][j] << " ";
}
cout << endl;
}
return C;
}
```

That's what I got so far...

Why can't I display MatrixC [0][3] and [1][3]??

#### Attached image(s)

Reputation: 0
• Posts: 8
• Joined: 30-April 12

## Re: Matrices multiplying by using vector.

Posted 01 May 2012 - 11:48 AM

Nevermind, I had brainfreeze. Thank you for all the help !