Find the index of a value which is a variable in an array

1 view (last 30 days)
Tyler Danzig
Tyler Danzig on 1 Dec 2021
Answered: Benjamin on 1 Dec 2021
I am trying to find the index of a value in an array which I used an equation to determine and saved as a variable. This is what I would like to happen, but I get an error:
azimuth_angle = 180 + atand(b/a); % The equation yields an azimuth_angle of 195, which is a value in the array azimuth_z.
azimuth_index = find(azimuth_z == azimuth_angle); % azimuth_index should give me 337, which is the index I want, but it only works when I replace azimuth_angle with the value it is (195).
Where azimuth_z is a 360x1 single and b and a are just values.
Is there a way to use find() with a variable? Or is there another function I need to use?

Accepted Answer

Benjamin on 1 Dec 2021
Sounds like azimuth_angle is not exactly 195, but it's something very close to 195.
So instead of using find to find an exact match, you can find values very close to 195, e.g.:
azimuth_index = find(abs(azimuth_z - azimuth_angle) <= 10*eps(azimuth_angle));
Or you can use min to find the closest match regardless of how close it is to 195:
[~,azimuth_index] = min(abs(azimuth_z - azimuth_angle));

More Answers (0)


Community Treasure Hunt

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

Start Hunting!