Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Order matrix elements as strings according to their value

1 回表示 (過去 30 日間)
Felipe Rivas
Felipe Rivas 2015 年 11 月 25 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
So I perform a series of calculations which give a 6x1 matrix, where the positions of the matrix are associated to a Team name.
X= [0.5, 0.7, 1, 5] when the X(1)=Team1 X(2)=Team2... X(4)=Team(4).
I wish to display a vector that would order the string in descending order according to the assigned value so in the specific example
Order=[Team4; Team3; Team2; Team1] which is according to the value
This is such that if the values of X change based on calculations the algorithm would display the team names in the order according to their numerical value.
Thank you,

回答 (2 件)

the cyclist
the cyclist 2015 年 11 月 25 日

Here's one way:

X = [0.5, 0.7, 1, 5];
teamList = {'Team1','Team2','Team3','Team4'};
[sortedX,sortingIndex] = sort(X,'descend');
sortedTeamList = teamList(sortingIndex)

Kirby Fears
Kirby Fears 2015 年 11 月 25 日
編集済み: Kirby Fears 2015 年 11 月 25 日
Using your example:
X= [0.5, 0.7, 1, 5];
Teams = {'Team1','Team2','Team3','Team4'};
You can sort X descending and keep the sorting index ( sortIdx ). You can index Teams with sortIdx to apply the same permutation that sorted X.
[X,sortIdx]=sort(X,'descend');
Teams = Teams(sortIdx);
Hope this helps.
  1 件のコメント
Felipe Rivas
Felipe Rivas 2015 年 11 月 25 日
Thank you very much, this helped a lot !!

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by