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

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 Comment

I have also done this without using the any() and just using find(), it still only produces 30 values

Sign in to comment.

 Accepted Answer

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.

More Answers (2)

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);
Hi both,
Thanks, they both work great

Categories

Asked:

D F
on 10 May 2018

Answered:

D F
on 10 May 2018

Community Treasure Hunt

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

Start Hunting!