Find function in Matlab
2 次查看(过去 30 天)
显示 更早的评论
Dear all ,
I have problem with this code
clc
close all
Signal = Hole1_mid;
Compare(:,1)=Signal(:,2);
value1 = 1.9752;
index1 = find(ismember(Compare,value1),n);
Hole1_6m(:,end) = Signal(1:index1,end);
Find function is not working in this case and I have no idea why
2 个评论
Rik
2017-12-19
What would you mean with 'not working'? If it is a true bug, it is very unlikely it wouldn't have been found yet in a popular function like find, but it's not impossible. If not a bug, you must not be understanding what it does.
Try to create a stand-alone piece of code we can run to replicate what you have. Point out what you get, and how it differs from what you want.
Stephen23
2017-12-20
编辑:Stephen23
2017-12-20
"Find function is not working in this case"
Actually it is working correctly.
"and I have no idea why"
Your code tests for equivalence of floating point values, which is a very buggy and unreliable way to write code. You need to change your algorithm to take into account floating point error:
and a thousand other threads where this has been discussed. You could easily have searched this forum yourself and found many answers telling what is happening. This is worth reading as well:
采纳的回答
Star Strider
2017-12-19
Try this:
tol_val = 0.001;
index1 = find(ismembertol(Compare,value1), n, tol_val);
See the documentation on ismembertol (link) for details. Choose the value for ‘tol_val’ that works with your data.
See the discussion in Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link) for the reason ismembertol is preferable in situations such as those you are experiencing.
0 个评论
更多回答(1 个)
Walter Roberson
2017-12-19
1.9752 is not exactly representable in binary floating point, so there is a good chance that whatever value is in Compare is not exactly the same as the bit-for-bit exact value stored in value1
Consider using ismembertol()
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!