Logical flaw while handling 2D arrays

This is a discussion on Logical flaw while handling 2D arrays within the C Programming forums, part of the General Programming Boards category; I have some Matlab m-code that involves matrix operations, which I had to convert to C code. I’ve reviewed my ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    25

    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.
    Last edited by CodeRed; 02-12-2012 at 02:27 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    j < 5?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    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.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Post the code for the trans_axis() and MatrixMult() functions too, if they aren't too long.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    612
    You have a 4x4 matrix, indexing starts at 0. Only valid indexes are 0-3.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    25
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design flaw in C++?
    By @nthony in forum C++ Programming
    Replies: 24
    Last Post: 10-20-2008, 07:47 PM
  2. design flaw
    By Bleech in forum Windows Programming
    Replies: 2
    Last Post: 10-23-2007, 11:10 PM
  3. yet another unexplainable flaw
    By stormbreaker in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2003, 12:56 PM
  4. is this a design flaw
    By blight2c in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 11:33 PM

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