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

5 vues (au cours des 30 derniers jours)
Tyler Danzig
Tyler Danzig le 1 Déc 2021
Réponse apportée : Voss le 1 Déc 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?

Réponse acceptée

Voss
Voss le 1 Déc 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));

Plus de réponses (0)

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by