How do I make two arrays go from largest to smallest?

1 vue (au cours des 30 derniers jours)
tommy sandler
tommy sandler le 28 Avr 2021
Réponse apportée : dpb le 28 Avr 2021
How do I put these in from largest to smallest?
a = [4 8 11 7];
b = ["voltage1", "voltage2", "voltage3", "voltage4"];
%this is the output I want
a = [11 8 7 4]
b = ["voltage3", "voltage2","voltage4", "voltage1"]
%this is what I tried
c = sort(a,'descend'); %this only makes these values in a greatest to smallest
%I am not sure how to make the string values go in that order

Réponse acceptée

dpb
dpb le 28 Avr 2021
[a,ix] = sort([4 8 11 7],'descend'); % keep the optional original order vector when sort
b = b(ix); % use it to arrange corollary variable in same order
NB: You don't need to keep the b array at all...just generate it on the fly from the order vector.
>> b=string("Voltage"+ix.')
b =
4×1 string array
"Voltage3"
"Voltage2"
"Voltage4"
"Voltage1"
>>

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by