Find function in Matlab
2 views (last 30 days)
Show older comments
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 Comments
Rik
on 19 Dec 2017
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
on 20 Dec 2017
Edited: Stephen23
on 20 Dec 2017
"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:
Accepted Answer
Star Strider
on 19 Dec 2017
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 Comments
More Answers (1)
Walter Roberson
on 19 Dec 2017
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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!