根据坐标对点进行分类

时间:2014-11-11 20:59:05

标签: performance matlab sorting grouping classification

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

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

方法#1

对于行数不错的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,您很可能不会有内存与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));

答案 1 :(得分:1)

我现在无法访问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

答案 2 :(得分:-1)

生成大量数据:

  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 秒。