フィルターのクリア

Permute a vector by a specific ordering

15 ビュー (過去 30 日間)
David
David 2014 年 7 月 15 日
編集済み: Azzi Abdelmalek 2014 年 7 月 15 日
I have two vectors of the same length x and y, and each value in y corresponds to its value in x by the same index number, e.g.
x=[1 3 2 5 4]
y=[.2 .6 .5 .4 .3]
form the ordered pairs (1, 0.2), (3, 0.6), etc. Now I'd like to sort the vector x, but want to preserve the relationship of the two vectors. One thing I tried but didn't work is
[X I]=sort(x)
Y=permute(y,I) % how to permute y by ordering given by I?
This doesn't change the ordering of y at all. For this example the answer should be
X=[1 2 3 4 5]
Y=[.2 .5 .6 .3 .4]
Any ideas?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 15 日
編集済み: Azzi Abdelmalek 2014 年 7 月 15 日
x=[1 3 2 5 4]
y=[.2 .6 .5 .4 .3]
[X I]=sort(x)
Y=y(I)

その他の回答 (1 件)

Ben11
Ben11 2014 年 7 月 15 日
You're almost there!
Try this:
[a b] = sort(x)
y = y(b)
y =
0.2000 0.5000 0.6000 0.3000 0.4000

カテゴリ

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