Link to home
Start Free TrialLog in
Avatar of mrperfect75000
mrperfect75000Flag for United States of America

asked on

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
How do i go about this? Thank you!
Avatar of mrperfect75000
mrperfect75000
Flag of United States of America image

ASKER

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




Avatar of Greybird
Greybird

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...
Thanks for ur resonce. What do u mean think of it as a column in my code?
Thank you for your help.
ASKER CERTIFIED SOLUTION
Avatar of Greybird
Greybird

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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