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

arrayarrayssort

Hi, in my program I have an array d, which is 100×1 and I have another 100×1 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.

Best Answer

Use the second return value of sort to reorder your e array.
[d, order] = sort(d);
e = e(order);