Find Command not working.
8 次查看(过去 30 天)
显示 更早的评论
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 :
How to solve this ?
0 个评论
回答(3 个)
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)
Walter Roberson
2021-10-24
T = [NaN 0.1:0.1:0.9]
a = 0.3;
Visually, it looks like 0.3 is in T
find(T == a)
But find() says it is not there.
Is it there? Let's look at the bit representation
fprintf('a:\n'); arrayfun(@pX, a);
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);
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
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 Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!