How can Fix the "find" with for loop

2 次查看(过去 30 天)
Hi, I am Struggling to get the hidden error. I just need to put the specific markers at specific points on the chirp graph. The problem is that it gives me the error " 1×0 empty double row vector" at specific line AND when I wrote this line manually to get the indices, I got the answer without error. is that bug or am I missing something? please help. I attach my code you can run and you will get the error at point (301) i.e (x= .3). if you write Y(X==.3), you'll get the answer although it gave you error in the code.
fs=1e3; X = 0:1/fs:2-(1/fs); fo = 1; f1 = 5; Y = chirp(X,fo,X(end),f1,'logarithmic'); points=zeros(2,20); xe =0:.1:X(end); for i=1:length(xe) i index = find(X==xe(i)) X_point=X(index); Y_point= Y(X==xe(i)); points(1,i)=X_point; points(2,i)=Y_point; plot(X,Y,X_point,Y_point,'*') labelstr = sprintf('%d',i); text(X(index)+.05,Y_point, labelstr); hold on end

回答(1 个)

Roger Stafford
Roger Stafford 2017-4-13
In your code you are assuming that the ‘find’ operation will always find an index from X that will match one in xe. The trouble is that the decimal fraction values in these vectors cannot be exactly represented since your computer uses binary numbers. Presumably the two representations of .3 in the two vectors are slightly different, yielding an empty status for 'index'. To correct this you should allow a tiny tolerance for such differences rather than requiring exact equality.
  3 个评论
ahmed youssef
ahmed youssef 2017-4-13
thank you for your answer, can you give me any suggestion gives me that tolerance.
Roger Stafford
Roger Stafford 2017-4-13
Use
index = find(abs(X-xe(i))<tol);
in place of
index = find(X==xe(i))
The ‘tol’ value should be smaller than the smallest difference that you want to elicit a false value and larger than the largest you tolerate for a true value. In your case these appear to be about .001 and 2*eps, respectively (eps = 2^(-52)), which leaves you a lot of room. I would choose tol = 10^(-12) in your case.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by