Matlab Coder C++ matrix multiplication issue

4 views (last 30 days)
Hello, I used Matlab coder to translate some Matlab code into C++. The C++ generated code produces different result from the original Matlab code. I traced the C++ code and found out the problem is with matrix multiplication. I tried a simple code to make sure the bug is not related to my Matlab code and still getting wrong result from generated code. Here is the Matlab code:
function [Final Delta] = Test_Coder(x,y,w)
Delta = x*y;
Final= Delta * w;
end
and this is the script that calls the function:
clear all
clc
x = [2,4;5,6];
y = [7,3;5,1];
w = [3;5];
[Final Delta] = Test_Coder(x,y,w)
Here is the generated C++ code ("printf" instructions are added later for debugging purpose):
// 'Test_Coder:3' Delta = x*y;
for (i0 = 0; i0 < 2; i0++) {
for (i1 = 0; i1 < 2; i1++) {
Delta[i0 + (i1 << 1)] = 0.0;
for (i2 = 0; i2 < 2; i2++) {
Delta[i0 + (i1 << 1)] += x[i0 + (i2 << 1)] * y[i2 + (i1 << 1)];
printf("\n Delta[%d] += x[%d] * y[%d] = %lf * %lf >> %lf ",i0 + (i1 << 1),i0 + (i2 << 1) ,i2 + (i1 << 1), x[i0 + (i2 << 1)],y[i2 + (i1 << 1)] ,Delta[i0 + (i1 << 1)]);
}
}
// 'Test_Coder:5' Final= Delta * w;
Final[i0] = 0.0;
for (i1 = 0; i1 < 2; i1++) {
Final[i0] += Delta[i0 + (i1 << 1)] * w[i1];
printf("\n Final[%d] += Delta[%d] * w[%d] = %lf * %lf >> %lf ",i0, i0 + (i1 << 1), i1, Delta[i0 + (i1 << 1)], w[i1], Final[i0]);
}
}
}
This is Matlab generated results:
Final =
152
300
Delta =
34 10
65 21
and here is C++ results:
Delta[0] += x[0] * y[0] = 2.000000 * 7.000000 >> 14.000000
Delta[0] += x[2] * y[1] = 5.000000 * 3.000000 >> 29.000000
Delta[2] += x[0] * y[2] = 2.000000 * 5.000000 >> 10.000000
Delta[2] += x[2] * y[3] = 5.000000 * 1.000000 >> 15.000000
Final[0] += Delta[0] * w[0] = 29.000000 * 15.000000 >> 435.000000
Final[0] += Delta[2] * w[1] = 15.000000 * 5.000000 >> 510.000000
Delta[1] += x[1] * y[0] = 4.000000 * 7.000000 >> 28.000000
Delta[1] += x[3] * y[1] = 6.000000 * 3.000000 >> 46.000000
Delta[3] += x[1] * y[2] = 4.000000 * 5.000000 >> 20.000000
Delta[3] += x[3] * y[3] = 6.000000 * 1.000000 >> 26.000000
Final[1] += Delta[1] * w[0] = 46.000000 * 15.000000 >> 690.000000
Final[1] += Delta[3] * w[1] = 26.000000 * 26.000000 >> 1366.000000
Any idea what is wrong and how to fix it? I am using Matlab R2015a.
Thanks, Ali
  3 Comments
Walter Roberson
Walter Roberson on 31 May 2015
I would change the format slightly
printf("\n Delta[%d] += x[%d] * y[%d] = %lf * %lf >> %lf ",i0 + (i1 << 1),i0 + (i2 << 1) ,i2 + (i1 << 1), x[i0 + (i2 << 1)],y[i2 + (i1 << 1)] ,Delta[i0 + (i1 << 1)]);
as a reminder that the values are being added to the existing and that what shows up last on the line is not the current multiplication but rather the cumulative sum for the location.
Ali Jooya
Ali Jooya on 1 Jun 2015
Thanks Walter. I changed the format.

Sign in to comment.

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 1 Jun 2015
Hi,
note that MATLAB uses column format for storing arrays, so you need to initialize
double X[]={2,5,4,6};
double Y[]={7,5,3,1};
to get the same results as in MATLAB.
Titus
  2 Comments
Ali Jooya
Ali Jooya on 2 Jun 2015
Thanks Titus. That solved the problem! This is a NN code I have converted into C++. It involves a lot of floating point number calculations. For low number of Epochs the results are the same, but over higher Epochs, e.g. 1000, I am getting different results. Any suggestion?
Titus Edelhofer
Titus Edelhofer on 3 Jun 2015
Edited: Titus Edelhofer on 3 Jun 2015
Hi Ali,
it depends on "different results". Note that numerical differences can always occur, so it would be necessary to see "how" different they are. With such a high number of Epochs I would be more astonished to see no differences ...
Titus

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!