sort a matrix in a specific way
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
dpb
am 3 Nov. 2022
I don't really see what is "sorted" about the result??? How did you arrive at that particular permutation?
Akzeptierte Antwort
Weitere Antworten (1)
Matt J
am 3 Nov. 2022
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 Kommentare
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!