How to arrange array in new order and change position of elements in corresponding array?
조회 수: 15 (최근 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.
댓글 수: 0
채택된 답변
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
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
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 Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!