Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Logical error converting Matlab matrix code to C

2 views
Skip to first unread message

Kate J.

unread,
Feb 12, 2012, 4:08:09 PM2/12/12
to
I apologize if this isn’t really a question that should be posted on the Mathworks forum, but I have some Matlab m-code that involves a lot of matrix operations, which I had to convert to C code. I’m hoping that there is someone on this forum who has both Matlab and C expertise. 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 calls to trans_axis() (output = 4x4 matrix of doubles), by the goalTM or actualTM matrices, which are also 4x4. The result is updated values for the 4x4 goalTM and actualTM matrices.

~ 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 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:

for j=1:5
goalTM=goalTM*trans_axis(rotaxis(j,1),goal_angles(j),translation(j,1));
end

---------------------------------------------------

My C 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 are incorrect, indicating that there's some problem in the management 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.

Kate J.

unread,
Feb 12, 2012, 4:16:10 PM2/12/12
to
note: please ignore all mentions of the "actualTM" matrix in my OP above; I had thought that I had eliminated all mentions of it, when I simplified my example code above...

James Tursa

unread,
Feb 13, 2012, 11:00:11 AM2/13/12
to
"Kate J." wrote in message <jh99np$9fl$1...@newscl01ah.mathworks.com>...
> I apologize if this isn’t really a question that should be posted on the Mathworks forum, but I have some Matlab m-code that involves a lot of matrix operations, which I had to convert to C code. I’m hoping that there is someone on this forum who has both Matlab and C expertise. 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 calls to trans_axis() (output = 4x4 matrix of doubles), by the goalTM or actualTM matrices, which are also 4x4. The result is updated values for the 4x4 goalTM and actualTM matrices.
>
> ~ 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 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:
>
> for j=1:5
> goalTM=goalTM*trans_axis(rotaxis(j,1),goal_angles(j),translation(j,1));
> end

Please post all of the relevant m-code for us to see. How, exactly, is MatrixMult called in your code? What, exactly, does MatrixMult do? Etc.

> ---------------------------------------------------
>
> My C 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);

Can you post your MatrixMult C-code?

> /* PROBLEM: the *first* out1[4][4] value is correct (showing that the MatrixMult() function is working properly), but the 2nd and subsequent out1 matrices are incorrect, indicating that there's some problem in the management 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 */

Are all of your matrix declarations the same?

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

Once you post more of your code we can comment on where the error(s) might be.

James Tursa

Phil Goddard

unread,
Feb 14, 2012, 10:04:46 AM2/14/12
to

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

What about the inputs the second time around?
Are they what you expect?

Phil.

Kate J.

unread,
Feb 14, 2012, 12:39:13 PM2/14/12
to
Thanks for your input, James and Phil.

>> What about the inputs the second time around?
>> Are they what you expect?
Yes, they are; that's what was so confusing. Sorry I didn't mention that previously.

I determined that I hadn't been re-initializing my out1[][] array properly; when I added the following line, all of the problems disappeared:

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

Thanks again for your responses.
0 new messages