find() isn't finding all the values in the array

조회 수: 4 (최근 30일)
D F
D F 2018년 5월 10일
답변: D F 2018년 5월 10일
Hi, I have an array (called 'delta') which has been read from excel. This array is of size 33x1. I have a second array (called 'interp_x_array') which I have created using linspace which is of size 1x2601. I want to find the indices of 'interp_x_array' for values in this array equal those in 'delta'.
There are 33 values that are indentical but when I use find(), it only output 30. I have checked these indices and they are correct, however it is missing out 3.
delta values are in a column array of [-22.2000, 14.2000, 16.7000, 19.5000, -22.4000, -15.2000, -1.4000, -6.2000, -1.8000, -73.9000, 29.4000, 3.8000, -6.0000, 22.8000, -20.1000, 32.2000, -1.1000, 1.8000, 15.5000, 51.3000, -119.0000, -71.3000, -82.3000, -21.1000, -11.7000, 16.6000, -5.3000, -37.1000, 37.0000,-2.2000, 10.8000, 3.3000, 1.4000]
if true
% interp_x_array = linspace(-130, 130, 2601);
% delta_mm = xlsread('a.xls', 1, 'F2:F35');
% delta = delta_mm * 1000;
% interp_x_array = round(interp_x_array, 1);
% delta_y_index = find(any(interp_x_array == delta));
end
  댓글 수: 1
D F
D F 2018년 5월 10일
I have also done this without using the any() and just using find(), it still only produces 30 values

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 10일
You are getting the error because of floating point error. To solve this problem use ismembertol() as follow
find(ismembertol(interp_x_array, delta))
it will return 33 elements.

추가 답변 (2개)

Akira Agata
Akira Agata 2018년 5월 10일
This is due to floating-point accuracy. For example:
>> interp_x_array(1079)
ans =
-22.2000
>> delta(1)
ans =
-22.2000
>> interp_x_array(1079) - delta(1)
ans =
-3.5527e-15
So, to find elements of delta which is in interp_x_array within tolerance, ismembertol function would be suitable, like:
[idx,loc] = ismembertol(delta,interp_x_array);

D F
D F 2018년 5월 10일
Hi both,
Thanks, they both work great

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by