Logical Indexing With LinSpace Issues

조회 수: 17 (최근 30일)
Lucas Graham
Lucas Graham 2023년 3월 8일
댓글: Walter Roberson 2023년 3월 9일
Logical indexing is missing an equivalence in a linspace array. I have absolutely no idea why or how to fix this. I'm running MATALB R2022b on Windows 11.
Any ideas or help or explanation would be appreciated.
Here is how you can recreate my issue:
X = linspace(0.2,3,29); %Creates an array going from 0.2 -> 3 via steps of 0.1. So 1.3 is in it.
disp(X(12))
1.3000
flag = (X == 1.3);
any(flag) %Yet for some reason 1.3 isn't detected.
ans = logical
0
%Just to be absolutely sure I'm not mad
disp(X(12))
1.3000
X(12) == 1.3
ans = logical
0
% Yet other non-integers still work!
flag = (X == 1.1);
any(flag)
ans = logical
1
  댓글 수: 2
Lucas Graham
Lucas Graham 2023년 3월 8일
Thank you for these resources!

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

채택된 답변

Star Strider
Star Strider 2023년 3월 8일
format long
X = linspace(0.2,3,29) %Creates an array going from 0.2 -> 3 via steps of 0.1. So 1.3 is in it.
X = 1×29
0.200000000000000 0.300000000000000 0.400000000000000 0.500000000000000 0.600000000000000 0.700000000000000 0.800000000000000 0.900000000000000 1.000000000000000 1.100000000000000 1.200000000000000 1.300000000000000 1.400000000000000 1.500000000000000 1.600000000000000 1.700000000000000 1.800000000000000 1.900000000000000 2.000000000000000 2.100000000000000 2.200000000000000 2.300000000000000 2.400000000000000 2.500000000000000 2.600000000000000 2.700000000000000 2.800000000000000 2.900000000000000 3.000000000000000
disp(X(12))
1.300000000000000
flag = (X == 1.3);
any(flag) %Yet for some reason 1.3 isn't detected.
ans = logical
0
Check_Equality = 1.3 - X(12)
Check_Equality =
2.220446049250313e-16
%Just to be absolutely sure I'm not mad
disp(X(12))
1.300000000000000
X(12) == 1.3
ans = logical
0
% Yet other non-integers still work!
flag = (X == 1.1);
any(flag)
ans = logical
1
The‘Check_Equality’ assignment demonstrates that the two numbers are actually not equal.
You’re not mad! You just haven’t been introduced to the subtle mysteries of floating-point numeric repreesentation.
See Floating-Point Numbers and Accuracy of Floating-Point Data for an extensive discussion.
.
  댓글 수: 2
Lucas Graham
Lucas Graham 2023년 3월 8일
Thank you!
Star Strider
Star Strider 2023년 3월 8일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

추가 답변 (1개)

Chris
Chris 2023년 3월 8일
편집: Chris 2023년 3월 8일
This looks like a problem of computer precision. Double-precision floating point numbers in matlab (and floating-point numbers in general) a̶r̶e̶ ̶n̶o̶t̶ ̶e̶x̶a̶c̶t̶ (edit: are a finite set of discrete values, so they are usually unlikely to exactly match the value we see displayed)
X = linspace(0.2,3,29);
Y = 0.2:0.1:3;
% X(11)==1.2 because the result of the linspace division
% is the same as the double representation for 1.2
sprintf('%.60f\n%.60f',X(11),Y(11))
ans =
'1.199999999999999955591079014993738383054733276367187500000000 1.199999999999999955591079014993738383054733276367187500000000'
% X(12) doesn't equal the double for 1.3.
sprintf('%.60f\n%.60f',X(12),Y(12))
ans =
'1.299999999999999822364316059974953532218933105468750000000000 1.300000000000000044408920985006261616945266723632812500000000'
You can use other comparison operators to test rough equivalence, e.g., (X(12)-1.3) < 1e-14
Here's a bit more information, if you're interested.
  댓글 수: 4
Lucas Graham
Lucas Graham 2023년 3월 8일
편집: Lucas Graham 2023년 3월 8일
My intuition is that using MATLAB's colon operator to build arrays is less likely to run into float point errors because it probably uses addition instead of division, is that correct?
For example, using like you did 0.2:0.1:3 vs. linspace(0.2,3,29).
Walter Roberson
Walter Roberson 2023년 3월 9일
No, the opposite. Each time you add two floating point numbers, the round-off error can increase (unless the sequence of operations has been carefully chosen.) The error for 0.1, 0.1+0.1, 0.1+0.1+0.1 is greater than for (1:3)/10

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by