필터 지우기
필터 지우기

eq() and == failing to find matches in generated array.

조회 수: 1 (최근 30일)
Jonathan G-H-Cater
Jonathan G-H-Cater 2021년 3월 10일
답변: David K. 2021년 3월 10일
I have generated the following vector:
x = -2:0.1:5;
But now when trying to mask the location of various values it seems to fail to find entries that are shown in memory when viewed manually. E.g.
max(x==1.1) % returns 1 as expected.
max(eq(x,1.1)) % returns 1 as expected.
max(x==1.8) % returns 0 but expected 1.
max(eq(x,1.8)) % returns 0 but expected 1.
x(39) % returns 1.8000 as a sanity check to prove it is there.
Im guessing this is some sort of floating point precision issue - but don't know how to avoid it in a reliable way as I thought that was the point of eq().
Have tried to make this example case as simple as possible - so sorry if it seems a little random.
  댓글 수: 1
Jonathan G-H-Cater
Jonathan G-H-Cater 2021년 3월 10일
Here is another one that fails... but now I really am confused:
max((abs(x-1.8)<eps)) % returns 0
max((abs(x-1.1)<eps)) % returns 1
Honestly expected that to solve precision issues.

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

채택된 답변

David K.
David K. 2021년 3월 10일
You are right that it is floating point precision. There are many ways to deal with this and your comment shows one of them with a slight adjustment:
max((abs(x-1.8)<=eps)) % returns 1
You can also use a function called ismembertol and do it as such:
max(ismembertol(x,1.8,eps)); % also returns 1

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by