Problem using the FIND function to identify the position of a negative decimal number.

조회 수: 3 (최근 30일)
Hello.
I need to find the position of a negative decimal number in a vector. The code I've used is as follows:
a = -2:0.05:0;
num = -0.95;
position = find(a==-0.95);
The problem is with some numbers like the one set in num. Although the number exists within the a vector, the result I get is:
1-0 empty double row vector
What could be the problem? and how can I fix it?. Thanks.
Best regards

채택된 답변

Matt J
Matt J 2019년 12월 28일
편집: Matt J 2019년 12월 28일
The numbers you are comparing differ by small floating point errors,
>> a(22)-(-0.95)
ans =
-1.1102e-16
Use ismembertol, instead.
>> [~,position]=ismembertol(num,a)
position =
22

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 12월 28일

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by