sorting a list by position

1 回表示 (過去 30 日間)
Lucy Hannah
Lucy Hannah 2019 年 5 月 1 日
編集済み: Guillaume 2019 年 5 月 1 日
p=[5 3 1 7 9 2 4 6 8]
o=[0 2 4 2 4 3 1 1 3]
I want to sort o from largest to smallest, but while sorting o, the movement position of o is also applied to the same position of p when sorting
so when aftersorted o=[4 4 3 3 2 2 1 1 0]
p will become [9 1 2 8 3 7 4 6 5]
or [1 9 8 2 7 3 6 4]
where in p the number 9 and 1 can be swap and is the same because from o there are two 4's that is in the same position as p
2 and 8 could be swap
3 and 7 can be swapped
4 and 6 can be swapped
5 cannot be swapped because only 1 number

採用された回答

Guillaume
Guillaume 2019 年 5 月 1 日
[newo, order] = sort(o, 'descend');
newp = p(order);
  2 件のコメント
Lucy Hannah
Lucy Hannah 2019 年 5 月 1 日
what is newo and newp
Guillaume
Guillaume 2019 年 5 月 1 日
編集済み: Guillaume 2019 年 5 月 1 日
Your new sorted o and sorted p. You can call them whatever you want, o and p respectively if you want to replace the original vectors.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by