Effacer les filtres
Effacer les filtres

Find function in Matlab

4 vues (au cours des 30 derniers jours)
Trung Ngo
Trung Ngo le 19 Déc 2017
Modifié(e) : Stephen23 le 20 Déc 2017
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 commentaires
Rik
Rik le 19 Déc 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.
Have a read here and here. It will greatly improve your chances of getting an answer.
Stephen23
Stephen23 le 20 Déc 2017
Modifié(e) : Stephen23 le 20 Déc 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:

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 19 Déc 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.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 19 Déc 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()

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by