# How to sort a vector based on the absolute difference of the sorted vector

조회 수: 2(최근 30일)
Baththama Alhassan 2021년 12월 14일
댓글: Baththama Alhassan 2021년 12월 20일
Example: a= [2,3,6,12,7,18,25,0,28,5]
b= [0,2,3,5,6,7,12,18,25,28]
absolutediffofb=[2,1,2,1,1,5,6,7,3]
arrangeb=[(2,3)(5,6)(6,7)(0,2)(3,5)(25,28)(7,12)(12,18)(18,25)]
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Baththama Alhassan 2021년 12월 16일
arrangeb are elements in b sorted based on the sorted absolute difference of b
a= [2,3,6,12,7,18,25,0,28,5]
b= [0,2,3,5,6,7,12,18,25,28]
absolutediffofb=[2,1,2,1,1,5,6,7,3]
Sortabsolutediffofb=[1,1,1,2,2,3,5,6,7]
arrangeb=(2,3)=1
(5,6)=1
(6,7)=1
(0,2)=2
(3,5)=2
(25,28)=3
(7,12)=5
(12,18)=6
(18,25)=7

댓글을 달려면 로그인하십시오.

### 채택된 답변

David Goodmanson 2021년 12월 14일
Hi Awais,
a= [2,3,6,12,7,18,25,0,28,5]
b = sort(a)' % column vector
c = [b(1:end-1) b(2:end)];
d = [diff(c,[],2) c];
e = sortrows(d)
e =
1 2 3
1 5 6
1 6 7
2 0 2
2 3 5
3 25 28
5 7 12
6 12 18
7 18 25
The ordered pairs are in columns 2 and 3. Some format like this is necessary since
[(2,3)(5,6)(6,7)(0,2)(3,5)(25,28)(7,12)(12,18)(18,25)]
is not part of Matlab syntax.
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Baththama Alhassan 2021년 12월 20일
@David Goodmanson thank you

댓글을 달려면 로그인하십시오.

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by