sorting a vector by another vector

50 ビュー (過去 30 日間)
fima v
fima v 2017 年 3 月 14 日
編集済み: Rik 2017 年 3 月 14 日
Hello , my X vector looks like this 12,13,14,15,16,17,18,2,3,4,5,6,7,8,9,10,11 my Y vector looks like 2.6926,0.2879,1.93,-0.631,0.041,0.007,-0.077,-4.55,-3.984,-0.604,0.13,-0.49,-1.67,-2.276,-0.641,0.524,1.5758
how can i sort the X vector in ascending order so Y values will stay on the same indexes of the X vector as before
so if for example in Vector X before sorting, X(1)=12 Y(1)=2.6926 , then after the sorting the values will stay on the same index and X(11)=12 Y(11)=2.6926.
Thanks

回答 (2 件)

ES
ES 2017 年 3 月 14 日
doc sort
do a sort on X. sort gives two outputs, the sorted array and the index for sorting.
Using the index for sorting, sort Y.
[Z,I] = sort(X);
Y(I)% Output required by you

Rik
Rik 2017 年 3 月 14 日
編集済み: Rik 2017 年 3 月 14 日
You can use the second output of the sort function to get the indices. Type doc sort for more information and options.
[X,order]=sort(X);
Y=Y(order);

カテゴリ

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