Wrong result given by matrix multiplication

10 次查看(过去 30 天)
I was doing a matrix multiplication with 3 matrices: BT * C * B
BT = [-1 0 3; 0 -3 -1; 4 0 -2; 0 -2 4; 1 0 3; 0 3 1; -4 0 2; 0 2 -4]
C = [32 8 0; 8 32 0; 0 0 12]
B = BT'
The result given by Matlab is:
>> BT * C * B
ans =
140 -12 -200 160 76 12 200 -160
-12 300 -72 144 -60 -300 72 -144
-200 -72 560 -160 56 72 -560 160
160 144 -160 320 128 -144 160 -320
76 -60 56 128 140 60 -56 -128
12 -300 72 -144 60 300 -72 144
200 72 -560 160 -56 -72 560 -160
-160 -144 160 -320 -128 144 -160 320
However, as I calculated the result by hand it gave me:
140 60 -56 -128 -140 -60 56 128
60 300 -72 144 -60 -300 72 -144
-56 -72 560 -160 56 72 -560 160
-128 144 -160 320 128 -144 160 -320
-140 -60 56 128 140 60 -56 -128
-60 -300 72 -144 60 300 -72 144
56 72 -560 160 -56 -72 560 -160
128 -144 160 -320 -128 144 -160 320
As you can see, Matlab gave wrong answers on
ans(2:end, 1)
and
ans(1, 2:end)
but correct on all the other elements ( i.e., ans(1, 1) and ans(2:end, 2:end) ) in the resulting 8-by-8 matrix.
I have checked my hand calculation several times and found nothing wrong in it.
Why do I get this weird behavior? Am I missing something?
Will be very appreciated if someone could correct/clarify me!

采纳的回答

Jan
Jan 2021-3-25
编辑:Jan 2021-3-25
The behavior is not weird, but Matlab calculates the correct results. If your manual computations give you another result, the error is on your side. Let's check this. What is your result for BT * C?
BT * C =
-32 -8 36
-24 -96 -12
128 32 -24
-16 -64 48
32 8 36
24 96 12
-128 -32 24
16 64 -48
Now multiply this with B:
-1 0 4 0 1 0 -4 0
0 -3 0 -2 0 3 0 2
3 -1 -2 4 3 1 2 -4
The element [2,1] is:
[-24, -96, -12] * [-1; 0; 3] = 24 + 0 - 36 = -12
So your 60 cannot be true.
Or do you start with C * B?
-32 -24 128 -16 32 24 -128 16
-8 -96 32 -64 8 96 -32 64
36 -12 -24 48 36 12 24 -48
Seriously, do you really think it is possible, that such a severe bug of Matlab has not been detected in the last 25 years? Matlab uses BLAS libraries to do this, which have been optimized by Intel. You can be sure, that they have tested the code exhaustively.
The is a pattern in your calculation:
ans(1, :) == -ans(5, :)
ans(:, 1) == -ans(:, 5)
This pattern does not occur in the real solution.
  3 个评论
hmhuang
hmhuang 2021-3-25
编辑:hmhuang 2021-3-25
Thank you very much for the answer. I found BT(1,3) was wronly initialized (should be -3 not 3) so as to the corresponding element in matrix B. That's why I got undesired (not wrong) result by matlab only in first row & column. (my fault)
Thank you for your time anyway!
Jan
Jan 2021-3-25
You are welcome. I had the same situation frequently enough to understand, that the computers seem to act illogical.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by