필터 지우기
필터 지우기

Matlab Coder C++ matrix multiplication issue

조회 수: 5 (최근 30일)
Ali Jooya
Ali Jooya 2015년 5월 31일
편집: Titus Edelhofer 2015년 6월 3일
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
Walter Roberson
Walter Roberson 2015년 5월 31일
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 2015년 6월 1일
Thanks Walter. I changed the format.

댓글을 달려면 로그인하십시오.

채택된 답변

Titus Edelhofer
Titus Edelhofer 2015년 6월 1일
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
Ali Jooya
Ali Jooya 2015년 6월 2일
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 2015년 6월 3일
편집: Titus Edelhofer 2015년 6월 3일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by