Outputing a correspond element of another variable

I want my final output to be the element A that is equivalent to B(max)
A = ["Bayo" "Tun" "s"]
B = [21, 45, 11]
example
max(B) = 45
How do I output the corresponding element in A (in it original string format).

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 30 Déc 2020
Modifié(e) : Ameer Hamza le 30 Déc 2020
Try this
A = ["Bayo" "Tun" "s"];
B = [21, 45, 11];
[~, idx] = max(B);
output = A(idx);
Result
>> output
output =
"Tun"

4 commentaires

Cutie
Cutie le 30 Déc 2020
Thank you Ameer Hamza. it works. Please can you explain what
[~, idx] = max(B); does? Particularly the '~'
max output two values. The first output is the maximum value, and the second is the index of the maximum value. ~ tells MATLAB to ignore the first output. You can also assign it to a variable if you are going to use it later
[max_value, idx] = max(B);
Cutie
Cutie le 30 Déc 2020
Thank you so much!
I am glad to be of help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by