sort a matrix in a specific way
2 次查看(过去 30 天)
显示 更早的评论
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
2022-11-3
I don't really see what is "sorted" about the result??? How did you arrive at that particular permutation?
采纳的回答
更多回答(1 个)
Matt J
2022-11-3
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,:)
3 个评论
另请参阅
类别
在 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!