Filter löschen
Filter löschen

== operator says two identical arrays aren't the same

7 Ansichten (letzte 30 Tage)
Kim Høg
Kim Høg am 13 Mär. 2015
Bearbeitet: Ken Atwell am 16 Mär. 2015
I have an array which I call graph and it is made like this.
graph = sin(1:pi/10:10*pi);
There's a variable called period_two.
period_two = 22;
Then period_two is used to make two arrays out of the first one.
graph(2:period_two-1)
graph(period_two:2*period_two-3)
And the output MatLab gives me is:
graph(2:period_two-1)
ans =
Columns 1 through 8
0.9672 0.9983 0.9317 0.7739 0.5403 0.2538 -0.0575 -0.3632
Columns 9 through 16
-0.6333 -0.8415 -0.9672 -0.9983 -0.9317 -0.7739 -0.5403 -0.2538
Columns 17 through 20
0.0575 0.3632 0.6333 0.8415
and
graph(period_two:2*period_two-3)
ans =
Columns 1 through 8
0.9672 0.9983 0.9317 0.7739 0.5403 0.2538 -0.0575 -0.3632
Columns 9 through 16
-0.6333 -0.8415 -0.9672 -0.9983 -0.9317 -0.7739 -0.5403 -0.2538
Columns 17 through 20
0.0575 0.3632 0.6333 0.8415
But if I use the == operator to check if the two arrays are the same, I get.
graph(2:period_two-1) == graph(period_two:2*period_two-3)
ans =
1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
Can you tell me why it says that the most of the values are not the same, when I can see that the two arrays are identical?

Akzeptierte Antwort

Ken Atwell
Ken Atwell am 13 Mär. 2015
Bearbeitet: Ken Atwell am 16 Mär. 2015
Floating point numbers are not infinitely precise, and you probably have differences too small to be noticed when displaying only 4 digits after the decimal.
Read this topic on determining equality, specifically the discussion about eps.
  1 Kommentar
James Tursa
James Tursa am 13 Mär. 2015
E.g., for this particular example,
>> num2strexact(graph(3:4))
ans =
'0.99834605415192101407484415176440961658954620361328125' '0.93171798264940119960186848402372561395168304443359375'
>> num2strexact(graph(23:24))
ans =
'0.99834605415192101407484415176440961658954620361328125' '0.931717982649401310624170946539379656314849853515625'

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by