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!