multiplying a matrix and vector:

This is a discussion on multiplying a matrix and vector: within the C Programming forums, part of the General Programming Boards category; the logic for multiplying a matrix: mat[row][col] and a vector : vec[row] is the following? Code: double *ans = malloc(rows ...

  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
    21,632
    Because you are using rows and cols instead of the entry indices.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,632
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,632
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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-08-2012, 11:13 PM
  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, 07:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21