# Thread: Logical flaw while handling 2D arrays

1. ## Logical flaw while handling 2D arrays

I have some Matlab m-code that involves matrix operations, which I had to convert to C code. I’ve reviewed my code’s logic numerous times, and can’t yet find the logical flaw that’s preventing my C code from matching my m-file’s values.

Here’s a description of the code, and of the bug:

~ the Matlab code multiplies the output of the call to trans_axis() (output = 4x4 matrix of doubles), by the goalTM matrix, which is also 4x4. The result is the updated value for the 4x4 goalTM matrix.

~ my C code seeks to reproduce the Matlab functionality. Since I didn’t think that it would be a good idea to call
MatrixMult(goalTM, TM1, goalTM);

… because it might result in overwriting the goalTM values that are the inputs to the function, I instead use a temporary 4x4 double array named out1[][], into which the MatrixMult() function’s outputs are copied. I then copy the out1[][] values back into the goalTM[][] array, before the for loop executes again.

Here’s the original m-code:

Code:
```for j=1:5
goalTM=goalTM*trans_axis(rotaxis(j,1),goal_angles(j),translation(j,1));
end```
My C code:

Code:
```for(j = 0; j < 5; j++){

/* call trans_axis() function, which writes output values into the TM1 array */
/* note: I have confirmed that trans_axis() is working properly and passes back the correct TM1 output matrix */

trans_axis(rotaxis[j][0],goal_angles[j],translation[j][0],TM1);

/* multiply the current goalTM[4][4] matrix  by the TM1[4][4] matrix, and return out1[4][4]  matrix */
MatrixMult(goalTM, TM1, out1);

/* PROBLEM: the *first* out1[4][4] value is correct (showing that the MatrixMult() function is working properly), but the 2nd and subsequent out1 matrices' values are incorrect, indicating some mismanagement of the variables. */

/* copy the output matrices back into the goalTM array */
for(a = 0; a < 4; a++){
for(b = 0; b < 4; b++){
goalTM[a][b] = out1[a][b];
}
}

} /* end for(j…) loop */```

Can anyone spot the logical flaw in my translation of Matlab code into C code? To restate the bug/problem, I get the correct result from the *first* call to MatrixMult(), but then the out1[][] output of the *second*, and all subsequent, calls to MatrixMult() are incorrect. Thanks in advance for your insights.

2. j < 5?

3. Without knowing how you've implemented the functions, it's a bit hard to guess the problem.

My guess is that one or both of the functions needs to reinitialise the matrices they are writing output to. Your trans_axis() presumably needs to reinitialise TM1 and MatrixMult() presumably needs to reinitialise out1.

Reinitialisation probably amounts to setting all elements to zero before commencing other calculations.

If either of your functions are using static storage (i.e. that persists between calls) that storage will also need to be reinitialised on each call.

Also, if out1 is a temporary (i.e. created locally within the caller) it is uninitialised by default. Creating any non-static variable or array local to a function does NOT implicitly initialise it (or elements) to zero.

4. Post the code for the trans_axis() and MatrixMult() functions too, if they aren't too long.

5. You have a 4x4 matrix, indexing starts at 0. Only valid indexes are 0-3.

6. Thanks for your input, everyone. I should have mentioned earlier that the rotaxis[][] array actually has the dimensions 5x3 (different than the other 4x4 arrays that I cited), so the for loop's j index value isn't a problem. It turns out that when I add a re-initialization line to the loop that I had included above:

Code:
```
for(a = 0; a < 4; a++){
for(b = 0; b < 4; b++){
goalTM[a][b] = out1[a][b];
out1[a][b] = 0.0;     /* new addition */
}
}
```

... the problem is solved! Thanks again.