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

4 次查看(过去 30 天)
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