簡體   English   中英

根據坐標對點進行分類

[英]Classifying points in accordance with their coordinates

A是3D(x,y,z)中幾個點的坐標矩陣。 例如:

A= [1.6 2.13 3; 1.2 2.36 5; 1.4 2.4 6; 1.01 2.21 9]

A = 1.6 2.13 3.0 1.2 2.36 5.0 1.4 2.40 6.0 1.01 2.21 9.0

我正在尋找一種將第二列( Y )的點分組為閾值0.09 “三個”組的“有效”解決方案。 手段:

GroupNumber = 3; threshold = (max(A(:,2))-min(A(:,2)))/GroupNumber;

Group{1} = 1.60 2.13 3.0 1.01 2.21 9.00 Group{2} = 1.2 2.36 5.0 Group{3} = 1.4 2.40 6.0

任何幫助將不勝感激!

方法1

對於A行數不錯的A ,您可能更喜歡矢量化解決方案-

GroupNumber = 3;

sorted_A = sortrows(A,2);
sorted_A_col2 = sorted_A(:,2);

limits = sorted_A_col2 + (max(sorted_A_col2) - sorted_A_col2)./GroupNumber;
matches = bsxfun(@le,sorted_A_col2,limits.'); %//'

[~,col_ind] = max(matches,[],2);
groups = arrayfun(@(x) sorted_A(col_ind == x,:), unique(col_ind),'Uniform',0);

顯示帶有給定輸入的celldisp(groups)的輸出-

groups{1} =
    1.6000    2.1300    3.0000
    1.0100    2.2100    9.0000
groups{2} =
    1.2000    2.3600    5.0000
groups{3} =
    1.4000    2.4000    6.0000

方法#2

對於A有大量行的A ,您很可能沒有剩余的內存可用於bsxfun並且您將不得不在這種情況下使用某種循環方法,因此效率不高。 以下可能是其中之一-

GroupNumber = 3;

sorted_A = sortrows(A,2);
sorted_A_col2 = sorted_A(:,2);
limits = sorted_A_col2 + (max(sorted_A_col2) - sorted_A_col2)./GroupNumber;

nrows = size(A,1);
prev_matches = false(nrows,1);
groups = cell(nrows,1);
for iter = 1:nrows
    curr_matches = sorted_A_col2<=limits(iter);
    groups{iter} = sorted_A(xor(curr_matches,prev_matches),:);
    prev_matches = curr_matches;
end
groups = groups(~cellfun('isempty',groups));

我現在無法訪問MATLAB,但大致是這樣的:

您可以按如下方式獲得第一組,

A= [1.6 2.13 3; 1.2 2.36 5; 1.4 2.4 6; 1.01 2.21 9]
B = A(:,2);
mean = (max(B)-min(B))/3;
C = B - min(B);
Group1 = A(C<mean,:)

然后從其余行中制作新矩陣,如下所示

A = A(C>=mean,:)

然后重復直到isempty(A) == true 雖然有許多優化可能。

編輯:

A= [1.6 2.13 3; 1.2 2.36 5; 1.4 2.4 6; 1.01 2.21 9]
while ~isempty(A)
    B = A(:,2);
    mean1 = (max(B)-min(B))/3;
    C = B - min(B);
    Group1 = A(C<mean1,:)
    A = A(C>=mean1,:)
    if size(A,1)==1
        break;
    end
end

結果將是:

A =

   1.6000   2.1300   3.0000
   1.2000   2.3600   5.0000
   1.4000   2.4000   6.0000
   1.0100   2.2100   9.0000

Group1 =

   1.6000   2.1300   3.0000
   1.0100   2.2100   9.0000

A =

   1.2000   2.3600   5.0000
   1.4000   2.4000   6.0000

Group1 =

   1.2000   2.3600   5.0000

A =

   1.4000   2.4000   6.0000

生成大量數據:

  A = 100 * rand(2000000,3);
    tic 
    GroupNumber = 100 ;` `% in hundred group
    Threshold = (length(A))/GroupNumber ;
    A = sortrows(A,2);
    Group = cell(GroupNumber,1);`

        for i = 1 : GroupNumber;

              if i == 1
           Group{i} = A(1:ceil(Threshold),:);     

           elseif i > 1 && i~= GroupNumber 
               if ceil((i-1)*Threshold) == ceil(Threshold)
                   bottum = ceil((i-1)*Threshold)+1;
               else
               end
               top=ceil(i*Threshold);
           Group{i} = A(bottum:top,:);

           elseif i == GroupNumber
               bottum = ceil((i-1)*Threshold);
               if ceil((i-1)*Threshold)<=ceil(i*Threshold) && ceil((i-1)*Threshold)>top

                   Group{i} = A(bottum:end,:);
               elseif  ceil((i-1)*Threshold)<=top

                   Group{i} = A(bottum+1:end,:);
               end

         end  
 end

    toc

經過的時間是1.160457秒。

對於建議的數據集,它類似於:

A = [1.6 2.13 3; 1.2 2.36 5; 1.4 2.4 6; 1.01 2.21 9];
GroupNumber = 3;

Group{1} =
     1.6000    2.1300    3.0000
     1.0100    2.2100    9.0000
Group{2} =
     1.2000    2.3600    5.0000 
Group{3} =
     1.4000    2.4000    6.0000

經過的時間是0.000010秒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM