Find function in Matlab
조회 수: 1 (최근 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에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!