MATLAB: Find(vector==value) not working

findfloating pointindexingmathematicsvectors

Why does find(vector==value) not work? I am trying to find the index of a value of 0.16 in a vector defined as vector=0:0.01:0.3. find(vector==0.16) should return 17 but returns an empty vector. Why?

Best Answer

You have encountered floating-point approximation error. See the documentation on Floating-Point Numbers for a full explanation.
Try this:
vector=0:0.01:0.3;
Result = find(vector<=0.16, 1, 'last');
producing:
Result =
17