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

조회 수: 1 (최근 30일)
tommy sandler
tommy sandler 2021년 4월 28일
답변: dpb 2021년 4월 28일
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

채택된 답변

dpb
dpb 2021년 4월 28일
[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"
>>

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by