Find Command not working.

조회 수: 7 (최근 30일)
Muhammad Owais
Muhammad Owais 2021년 10월 24일
댓글: Image Analyst 2021년 10월 24일
Hi everyone . I have a matrix with first row as T=[NaN 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]. When I use the command now if i have a=0.6 and i try to find index of 'a' as V=find(T==a). it gives me an answer as :
1×0 empty double row vector
How to solve this ?

답변 (3개)

Chunru
Chunru 2021년 10월 24일
use abs(difference) to comapare the float numbers.
T=[NaN 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9];
a=0.6;
V=find(abs(T-a)<1e-10)
V = 7
  댓글 수: 1
Muhammad Owais
Muhammad Owais 2021년 10월 24일
Yes Thanks . This worked!

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


Image Analyst
Image Analyst 2021년 10월 24일
It's probably not exactly 0.6. Try ismembertol().

Walter Roberson
Walter Roberson 2021년 10월 24일
T = [NaN 0.1:0.1:0.9]
T = 1×10
NaN 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000
a = 0.3;
Visually, it looks like 0.3 is in T
find(T == a)
ans = 1×0 empty double row vector
But find() says it is not there.
Is it there? Let's look at the bit representation
fprintf('a:\n'); arrayfun(@pX, a);
a: 3FD3333333333333
So for the literal, the bit representation ends in all hex 3's.
Is that bit representation present in T?
fprintf('\nT:\n'); arrayfun(@pX, T);
T: FFF8000000000000 3FB999999999999A 3FC999999999999A 3FD3333333333334 3FD999999999999A 3FE0000000000000 3FE3333333333333 3FE6666666666666 3FE999999999999A 3FECCCCCCCCCCCCD
No. There is a very similar bit representation that ends in a 4 rather than a 3. The last 4 bits of the actual value are 0011 (decimal 3), but the closest that T has is something for which the last 4 bits are 0100 (decimal 4).
When you calculate something two different ways, the bits are not necessarily going to come out identical.
function pX(N)
fprintf('%016X\n', typecast(N, 'uint64'));
end
  댓글 수: 1
Image Analyst
Image Analyst 2021년 10월 24일
A thorough explanation. There is also an explanation in the FAQ:
This is stuff you would have learned in your numerical analysis/linear algebra class at the university if you had taken such a class.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by