Thread: multiplying a matrix and vector:

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    multiplying a matrix and vector:

    the logic for multiplying a matrix: mat[row][col] and a vector : vec[row]

    is the following?

    Code:
    double *ans = malloc(rows * sizeof (double));    
    int i;    
        for (i=0; i<rows; rows++)
            for (i=0; i<cols; cols++)
                ans[rows] = ans[rows] + mat[rows][cols] * vec[rows];
    It does not give the right answer.. anyone know what is wrong?
    I used this site for the formula: Matrix-Vector Algebra

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because you are using rows and cols instead of the entry indices.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    oh thanks, well i changed to:
    Code:
    #include <stdlib.h>
    /* Multiply a matrix by a vector. */
    double *matrix_vector_multiply(int rows, int cols,
                                   double **mat, double *vec)
    {
    
            double *ans = malloc(rows * sizeof (double));
    
            int i,j;
            for (i=0; i<rows; rows++)
            ans[i]=0;
    
            for (i=0; i<rows; rows++){
    
    
                for (j=0; j<cols; cols++)
                     {
                    ans[i] =  mat[i][j]*vec[i] + mat[i][j+1]*vec[i];
                    }
                }
            return ans;
    }
    


    why is it still not working?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because you are not adding the right things. Look carefully at the article that you linked to, in particular the animated graphic that circles the terms that are multiplied and added.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    for (i=0; i<rows; rows++){

    for (j=0; j<cols; cols++)
    {

    ans[i] = ans[i] + mat[i][j] * vec[j];
    }
    }

    still 0s

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That looks more like it, though you still have to test to check

    By the way, you did well to post in [code][/code] bbcode tags in your first post. Don't stop, and don't introduce special syntax highlighting beyond what the forum software provides to us.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiplying Two Matrix!
    By alireza beygi in forum C Programming
    Replies: 5
    Last Post: 01-09-2012, 12:13 AM
  2. multiplying a matrix inputed from a file
    By eater in forum C Programming
    Replies: 16
    Last Post: 04-21-2009, 09:34 AM
  3. matrix to vector
    By izzy in forum C Programming
    Replies: 1
    Last Post: 03-21-2009, 07:00 PM
  4. matrix multiplying
    By noodle24 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 02:17 PM
  5. Multiplying tangent and bitangent by scale matrix
    By psychopath in forum Game Programming
    Replies: 6
    Last Post: 02-08-2006, 08:28 AM