sort a matrix in a specific way

Hi,
I have two matrices that correspond to points coordinates and I want to sort their concatenation in a way illustrated below as following:
%first matrix
A = [0.1 0;
0.5 0;
1 0.3;
1 0.7;
0.6 1;
0.1 1;
0 0.9;
0 0.2];
%second matrix
B = [0 0;
1 0;
1 1;
0 1];
%concatenate
C = [A ; B];
%sort matrix C in a way that all cooridnates are sorted in a square-like manner and looks like:
% C = [0 0;
% 0.1 0;
% 0.5 0;
% 1 0;
% 1 0.3;
% 1 0.7;
% 1 1;
% 0.6 1;
% 0.1 1;
% 1 0;
% 1 0.9;
% 0 0.2];
Any help would be appreicted.

2 件のコメント

dpb
dpb 2022 年 11 月 3 日
I don't really see what is "sorted" about the result??? How did you arrive at that particular permutation?
LH
LH 2022 年 11 月 3 日
All coordinates are in a square-like order. If you imagine you have a square with vertices x=[0 1 1 0] and y[0 0 1 1], then if you have points along its edges, they must be order like matrix C.

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2022 年 11 月 3 日
編集済み: Matt J 2022 年 11 月 3 日

0 投票

%first matrix
A = [0.1 0;
0.5 0;
1 0.3;
1 0.7;
0.6 1;
0.1 1;
0 0.9;
0 0.2];
%second matrix
B = [0 0;
1 0;
1 1;
0 1];
%concatenate
C = [A ; B];
k = convhull(C);
C=C(k,:)
C = 13×2
0.1000 0 0.5000 0 1.0000 0 1.0000 0.3000 1.0000 0.7000 1.0000 1.0000 0.6000 1.0000 0.1000 1.0000 0 1.0000 0 0.9000

4 件のコメント

LH
LH 2022 年 11 月 3 日
Is there a reason behind some of the rows might disappear when using this method?
Matt J
Matt J 2022 年 11 月 3 日
Some of the points are not perfectly on the boundary of the square.
LH
LH 2022 年 11 月 3 日
Thanks, makes sense. Is there another command/method I can use to do the extact sorting but with keeping all points? If not, I believe coding this will be the only way.
Matt J
Matt J 2022 年 11 月 3 日
I have given a second answer which should keep all of the points.

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2022 年 11 月 3 日

0 投票

A = [0.1 0;
0.5 0;
1 0.3;
1 0.7;
0.6 1;
0.1 1;
0 0.9;
0 0.2];
%second matrix
B = [0 0;
1 0;
1 1;
0 1];
%concatenate
C = [A ; B];
D=normalize(C,'center');
[~,k]=sort(atan2(D(:,2),D(:,1)));
C=C(k,:)
C = 12×2
0 0.2000 0 0 0.1000 0 0.5000 0 1.0000 0 1.0000 0.3000 1.0000 0.7000 1.0000 1.0000 0.6000 1.0000 0.1000 1.0000

3 件のコメント

LH
LH 2022 年 11 月 3 日
Many thanks for this. Although the first rows should be placed at the end of the matrix (if we consider going anticlockwise starting from the bottom edge of the square), but this method is better since it keeps all element.
Matt J
Matt J 2022 年 11 月 3 日
You can use circshift to choose the starting point for the list.
LH
LH 2022 年 11 月 4 日
Great. Thanks.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

LH
2022 年 11 月 3 日

コメント済み:

LH
2022 年 11 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by