MEX File Generates Incorrect Results
If your program generates the wrong answers, consider the following.
Check for errors in the computational logic.
Check if the program reads from an uninitialized section of memory. For example, reading the 11th element of a 10-element vector yields unpredictable results.
Check if the program overwrites valid data due to memory mishandling. For example, writing to the 15th element of a 10-element vector overwrites data in the adjacent variable in memory. This case can be handled in a similar manner as segmentation violations.
In all these cases, you can use
mexPrintf to examine data values
at intermediate stages. Alternatively, run MATLAB® within a debugger.