order the rows of the 3 matrices starting from the third coordinate of the point P

2 views (last 30 days)
Hi! I have to order the rows of the 3 matrices starting from the third coordinate of the point P.
A = [28.0445 -17.2717 83.972 1 2 3
27.8199 -16.986 83.3748 4 5 6
27.2756 -16.339 81.871 7 8 9
25.3805 -14.0329 67.019 10 11 12];
B = [25.3805 -14.0329 67.019 13 14 15
25.3425 -14.0698 66.0921 16 17 18
25.2731 -14.1203 65.1073 19 20 21
25.1719 -14.1617 64.1436 22 23 24];
C = [25.3805 -14.0329 67.019 25 26 27
26.2863 -14.4669 67.2675 28 29 30
27.0031 -14.7142 67.6157 31 32 33
27.7458 -14.9233 68.1745 34 35 36];
P = [25.3805 -14.0329 67.019];
So the final result would be:
A_new = [25.3805 -14.0329 67.019 10 11 12
27.2756 -16.339 81.871 7 8 9
27.8199 -16.986 83.3748 4 5 6
28.0445 -17.2717 83.972 1 2 3];
B_new = [25.3805 -14.0329 67.019 13 14 15
25.3425 -14.0698 66.0921 16 17 18
25.2731 -14.1203 65.1073 19 20 21
25.1719 -14.1617 64.1436 22 23 24]; % same as B
C_new = [25.3805 -14.0329 67.019 25 26 27
26.2863 -14.4669 67.2675 28 29 30
27.0031 -14.7142 67.6157 31 32 33
27.7458 -14.9233 68.1745 34 35 36]; % same as C
P = [25.3805 -14.0329 67.019];

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 5 Oct 2023
Edited: Dyuman Joshi on 5 Oct 2023
Use sort
A = [28.0445 -17.2717 83.972 1 2 3
27.8199 -16.986 83.3748 4 5 6
27.2756 -16.339 81.871 7 8 9
25.3805 -14.0329 67.019 10 11 12];
B = [25.3805 -14.0329 67.019 13 14 15
25.3425 -14.0698 66.0921 16 17 18
25.2731 -14.1203 65.1073 19 20 21
25.1719 -14.1617 64.1436 22 23 24];
C = [25.3805 -14.0329 67.019 25 26 27
26.2863 -14.4669 67.2675 28 29 30
27.0031 -14.7142 67.6157 31 32 33
27.7458 -14.9233 68.1745 34 35 36];
P = [25.3805 -14.0329 67.019];
%Sort according to distance from the 3rd coordinate of P
[~,idx1]=sort(abs(A(:,3)-P(3)));
[~,idx2]=sort(abs(B(:,3)-P(3)));
[~,idx3]=sort(abs(C(:,3)-P(3)));
A_new = A(idx1,:)
A_new = 4×6
25.3805 -14.0329 67.0190 10.0000 11.0000 12.0000 27.2756 -16.3390 81.8710 7.0000 8.0000 9.0000 27.8199 -16.9860 83.3748 4.0000 5.0000 6.0000 28.0445 -17.2717 83.9720 1.0000 2.0000 3.0000
B_new = B(idx2,:)
B_new = 4×6
25.3805 -14.0329 67.0190 13.0000 14.0000 15.0000 25.3425 -14.0698 66.0921 16.0000 17.0000 18.0000 25.2731 -14.1203 65.1073 19.0000 20.0000 21.0000 25.1719 -14.1617 64.1436 22.0000 23.0000 24.0000
C_new = C(idx3,:)
C_new = 4×6
25.3805 -14.0329 67.0190 25.0000 26.0000 27.0000 26.2863 -14.4669 67.2675 28.0000 29.0000 30.0000 27.0031 -14.7142 67.6157 31.0000 32.0000 33.0000 27.7458 -14.9233 68.1745 34.0000 35.0000 36.0000

More Answers (1)

Fabio Freschi
Fabio Freschi on 5 Oct 2023
If you want to sort A according to the third column you can simply do the following
% your data
A = [28.0445 -17.2717 83.972 1 2 3
27.8199 -16.986 83.3748 4 5 6
27.2756 -16.339 81.871 7 8 9
25.3805 -14.0329 67.019 10 11 12];
B = [25.3805 -14.0329 67.019 13 14 15
25.3425 -14.0698 66.0921 16 17 18
25.2731 -14.1203 65.1073 19 20 21
25.1719 -14.1617 64.1436 22 23 24];
C = [25.3805 -14.0329 67.019 25 26 27
26.2863 -14.4669 67.2675 28 29 30
27.0031 -14.7142 67.6157 31 32 33
27.7458 -14.9233 68.1745 34 35 36];
% engine
[~,idx] = sort(A(:,3))
A_new = A(idx,:);
similarly for B and C

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!