I need to multiply a matrix and a vector.
To accomplish that I have wrote a function with parameters :
float** M The maxtrix of dimensions : m x n.
float* V The vector of length n.
float* R Where I store the result, vector of length m, already allocated.
int m, int n The lengths.
Here is my code :
int i,j;
for (i=0;i<m;i++){
for (j=0;j<n;j++){
R[i]+=(M[i][j]*V[j]);
}
}
The complete function code :
void m_mult_v(float** M, float* V, float* R, int m, int n) {
int i,j;
for (i=0;i<m;i++){
for (j=0;j<n;j++) {
R[i]+=(M[i][j]*V[j]);
}
}
}
The problem is that the result I got is not the right one. :-/ any idea ?
Thanks for your future answers !
EDIT
Solution found thanks for your tips !
I just added this portion of code, to set R to all zeros.
for (i=0;i<m;i++){
R[i] = 0;
}
You forgot to initialize R? If it’s stack-allocated or allocated with malloc() its initial state is not defined.