How to arrange array in new order and change position of elements in corresponding array?

19 ビュー (過去 30 日間)
Hi, in my program I have an array d, which is 100x1 and I have another 100x1 array e, that corresponds to d. I would like help to sort d in ascending order (d=sort(d);) and also change the positions of the elements in e such that the result still corresponds to the element positions in d. For instance if the smallest value in d is the fourth element it becomes the first element in d and the forth element in e changes position with the first element. Thanks.

採用された回答

Guillaume
Guillaume 2015 年 4 月 17 日
Use the second return value of sort to reorder your e array.
[d, order] = sort(d);
e = e(order);

その他の回答 (1 件)

Star Strider
Star Strider 2015 年 4 月 17 日
You can simply sort ‘e’ as well:
d = sort(d);
e = sort(e);
This assumes of course that all of the elements of ‘d’ are greater than zero.
  2 件のコメント
ajk1
ajk1 2015 年 4 月 17 日
編集済み: ajk1 2015 年 4 月 17 日
thanks, if I have another array f that also corresponds to d but the magnitude of the values do not increase in the same way as the elements in d is there a way of changing the order to also correspond to d?
Star Strider
Star Strider 2015 年 4 月 17 日
The easiest way is to do whatever operation created ‘f’ on the elements of sorted vector ‘d’.

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

カテゴリ

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