sorting an array in a specific order

조회 수: 2 (최근 30일)
Max
Max 2022년 8월 10일
댓글: Max 2022년 8월 10일
How would I sort array elements in ascending order based on how close they are to a specific value. For example if x=[1 2 2.5 3 4 5] and y= 3.4 how would I make x arrange as [3 4 2.5 2 5 1]?

채택된 답변

Matt J
Matt J 2022년 8월 10일
편집: Matt J 2022년 8월 10일
x=[1 2 2.5 3 4 5]; y= 3.4;
[~,is]=sort(abs(x-y))
is = 1×6
4 5 3 2 6 1
x=x(is)
x = 1×6
3.0000 4.0000 2.5000 2.0000 5.0000 1.0000

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by