Sort vector based on another - How?

2 ビュー (過去 30 日間)
Annie
Annie 2025 年 4 月 29 日
回答済み: Walter Roberson 2025 年 4 月 29 日
So lets say I have a vector with distances ranging from 1 to 5 , whole numbers.
l = [ 5 3 2 1]
Associated with this vector are random integer values.
k = [ 2 3 4 1]
I want to be able to sort I and the k vectors follow....
so
l = [ 1 2 3 5]
k = [1 4 3 2]
How do I do this?

採用された回答

Walter Roberson
Walter Roberson 2025 年 4 月 29 日
l = [ 5 3 2 1]
l = 1×4
5 3 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
k = [ 2 3 4 1]
k = 1×4
2 3 4 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[l_sorted, l_sortidx] = sort(l);
k_sorted = k(l_sortidx);
l_sorted
l_sorted = 1×4
1 2 3 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
k_sorted
k_sorted = 1×4
1 4 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by