Question about the summation of array of elements with complex double

24 vues (au cours des 30 derniers jours)
Moses Tang
Moses Tang le 8 Mar 2019
Commenté : Moses Tang le 8 Mar 2019
Hi there, I have a strange problem when doing the summation for my array.
I got a array of 1x64 complex double. I would like to calculate the sum of those 64 elements.
However the result come out was quite strange and I don't know where I got wrong....
'sle' is the name of the data.
I did try with real(sle) and imag(sle). Although real(sle) did give me kind of a sensible answer(I suppose), in imag(sle) from element 33 to element 40 did show a imaginary value. Which is strange because when I look back at the data, there was no corresponding imaginary numbers that present in sle.
Anyone know the reason why? Please help... ><
sle =
1 7
0.1250 + 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i
8 14
0.1250 + 0.0000i -0.1250 + 0.0000i -0.1250 + 0.0000i -0.1250 + 0.0000i -0.1250 + 0.0000i -0.1250 + 0.0000i -0.1250 + 0.0000i
15 21
-0.1250 + 0.0000i -0.1250 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i
22 28
0.3750 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i
29 35
-0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i -0.1250 + 0.0000i -0.1250 - 0.0000i -0.1250 + 0.0000i
36 42
-0.1250 - 0.0000i -0.1250 + 0.0000i -0.1250 - 0.0000i -0.1250 + 0.0000i -0.1250 - 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i
43 49
0.1250 + 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i 0.1250 + 0.0000i -0.3750 + 0.0000i
50 56
-0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i -0.3750 + 0.0000i
57 63
0.3750 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i 0.3750 + 0.0000i
64
0.3750 + 0.0000i
sum(sle)
ans =
-1.7764e-15
  4 commentaires
Moses Tang
Moses Tang le 8 Mar 2019
Thank you very much. It works!!!
Stephen23
Stephen23 le 8 Mar 2019
Modifié(e) : Stephen23 le 8 Mar 2019
"Which is strange because when I look back at the data, there was no corresponding imaginary numbers that present in sle."
It is not strange at all, because your data actually does contain non-zero imaginary values.
It is easy to show those imaginary values:
>> format long
>> tmp = imag(sle(:));
>> tmp(tmp>0)
ans =
1.540743955509789e-033
1.540743955509789e-033
1.540743955509789e-033
1.540743955509789e-033
>> find(tmp>0)
ans =
33
35
37
39
"Even I try to sum up the real part of the data, it still gives me strange results."
"Why does it goes to e-15 when it is just addition of 64 small elements...."
It is not strange at all. You are summing positive and negative values, which almost enttirely cancel out when they are added together. This is also easy to show:
>> sum(tmp(tmp>0)) % positive
ans = 6.162975822039155e-033
>> sum(tmp(tmp<0)) % negative
ans = -6.162975822039155e-033
What do you expect the output to be, when those values are added together? (hint: zero plus some floating point error).
So far you have shown perfectly normal summation, and nothing that appears strange at all.
It appears that you wrote some code which does not take floating point error into account: you need to learn about how to handle the results of floating point operations, otherwise you will always be confused by the values that computers calculate.

Connectez-vous pour commenter.

Réponse acceptée

madhan ravi
madhan ravi le 8 Mar 2019
sum(sym(m))
  2 commentaires
Stephen23
Stephen23 le 8 Mar 2019
Modifié(e) : Stephen23 le 8 Mar 2019
Note that this does not actually explain the cause of what the OP observes, which appears to be entirely due to floating point error. The uploaded data is not symbolic.
Moses Tang
Moses Tang le 8 Mar 2019
I will have a look on it. Thanks for everything ^^

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by