Solved

Multiplying a column vector with matrix in C++

Posted on 2005-05-06
3,659 Views
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
Question by:mrperfect75000

Author Comment

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

LVL 5

Expert Comment

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 Comment

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

LVL 5

Accepted Solution

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

LVL 4

Expert Comment

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!