• Status: Solved
• Priority: Medium
• Security: Public
• Views: 3713

# Multiplying a column vector with matrix in C++

I have this program here that works fine multiplying a row vector with a matrix. However i want to multiply a column vector with a matrix...i.e instead of a=[1,1,1,1,1] ......
i want
a = 1
1
1
1
1
0
mrperfect75000
• 2
• 2
1 Solution

Author Commented:
Here is my code

#include <iostream.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;
#define MAX 5

void mulmatvec(int m, int n, double a[MAX][MAX], double x[MAX], double b[MAX]){
int i,j;

for(i = 0; i < m; i++){
b[i] = 0.;
for(j = 0; j < n; j++)
b[i] += a[MAX][MAX]*x[j];
}
return;
}

int main(){
int i,j=5,m=5,n=5;
double  x[MAX], b[MAX];

double a[MAX][MAX]=
{{0.900,0.100,0.000,0.000,0.000},
{0.000,0.400,0.600,0.000,0.000},
{0.000,0.600,0.300,0.100,0.000},
{0.000,0.000,0.000,0.000,1.000},
{0.000,0.150,0.000,0.850,0.000}};

cout << "Enter the vector\n";
for(j = 0; j < n; j++)
cin >> x[j];
mulmatvec(m,n,a,x,b);

cout << "\nA*x = \n";
for(i = 0; i < m; i++)
cout << b[i]<< "\n";

return 0;
}

0

Commented:
You don't really need to have it as a column. You just have to think of it as a column in your code.
If you really want to have it as a column, make a two-dimensionnal matrix with a dimension to 1
double x[5][1] for example...
0

Author Commented:
Thanks for ur resonce. What do u mean think of it as a column in my code?
0

Commented:
I mean that if you store the vector in an uni-dimensionnal array, it's up to you to view this dimension as a "vertical" one or a "horizontal" one

Then you just have to code keeping this fact in mind (i.e. x[2] is the 3rd line element, instead of the 3rd column element).
0

Commented:
For matrix multiplication C=AxB where total number of columns of matrix A should be equal to total number of rows of matrix B.
For multiply a row vector to matrix is possible. For this number of elements of row vector should be equal to total number of rows of a matrix.
HOw you multiply column vector to matrix? matrix should have only one row. then you can do it. Means you are multiplying column vector to row vector(matrix).
A=           B=3 2 1           C= 3 2 1
1                                         6 4 2
2                                         9 6 3
3
For these every thing you can use matrix multiplication method.
-Mahesh
0

## Featured Post

• 2
• 2
Tackle projects and never again get stuck behind a technical roadblock.