Multiplying a column vector with matrix in C++

Posted on 2005-05-06
Last Modified: 2013-11-17
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
How do i go about this? Thank you!
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];

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

      double a[MAX][MAX]=

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

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

      return 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...

    Author Comment

    Thanks for ur resonce. What do u mean think of it as a column in my code?
    Thank you for your help.
    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).
    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
    For these every thing you can use matrix multiplication method.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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 ( 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.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now