Matlab:如何输出按距离排序的矩阵

邓永信

我在3D点云中有一簇点

    A = [ 1 4 3;
    1 2 3;
    1 6 3;
    1 5 3];

然后找到距离矩阵:

    D= pdist(A);
    Z= squareform(D);
    Z =

 0     2     2     1
 2     0     4     3
 2     4     0     1
 1     3     1     0

我想对这些点进行排序,以使通过这些点的距离之和最小,并输出到另一个矩阵中。这与TSP问题类似,但在3D模型中。有什么功能可以做到这一点吗?预先感谢您的帮助。

迪卡卡

这可能是一种方法,并且对于多种数据大小必须足够高效-

D = pdist(A);
Z = squareform(D);  %// Get distance matrix

N = size(A,1);      %// Store the size of the input array for later usage
Z(1:N+1:end) = Inf; %// Set diagonals as Infinites as we intend to find
                    %// minimum along each row

%// Starting point and initialize an array to store the indices according
%// to the sorted requirements set in the question
idx = 1;
out_idx = zeros(N,1);
out_idx(1) = idx;

%// Perform an iterative search to look for nearest one starting from point-1
for k = 2:N
    start_ind = idx;
    [~,idx] = min(Z(start_ind,:));
    Z(:,start_ind) = Inf;
    out_idx(k) = idx;
end

%// Now that you have the list of indices based on the next closest one, 
%// sort the input array based on those indices and have the desired output 
out = A(out_idx,:)

给定输入的样本运行-

A =
     1     4     3
     1     2     3
     1     6     3
     1     5     3
     1     2     3
out =
     1     4     3
     1     5     3
     1     6     3
     1     2     3
     1     2     3

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Django按距离排序

来自分类Dev

按距离对UITableView进行排序

来自分类Dev

如何按总计字段对SSRS矩阵报表进行排序

来自分类Dev

如何按距查询点的距离排序地理空间查询

来自分类Dev

如何按到给定点的距离排序点列表?

来自分类Dev

Matlab:按行排序

来自分类Dev

按矩阵和输出索引的子集矩阵

来自分类Dev

tableview按距离快速排序

来自分类Dev

按列对矩阵排序

来自分类Dev

如何输出按用户排序的命令列表?

来自分类Dev

按列名排序矩阵

来自分类Dev

在Matlab中,找到每个矩阵元素的距离

来自分类Dev

NSMutableArray数据按距离排序

来自分类Dev

Matlab如何对向量和矩阵进行按位与运算?

来自分类Dev

MongoDB:如何考虑多个字段按距离排序?

来自分类Dev

正确按向量排序矩阵

来自分类Dev

Matlab:如何输出按距离排序的矩阵

来自分类Dev

如何按行名对矩阵排序?

来自分类Dev

在R中:如何按行名索引对矩阵进行排序?

来自分类Dev

如何按与用户的距离对业务对象数组进行排序?

来自分类Dev

按列排序输出

来自分类Dev

如何按距离对这个 Solr 查询进行排序?

来自分类Dev

R 数据输出按距聚类中心的距离排序

来自分类Dev

如何从R中的距离矩阵生成排序图

来自分类Dev

与python一起使用时,如何处理来自谷歌地图距离矩阵api的输出?

来自分类Dev

按距离多重排序

来自分类Dev

如何在angularjs中按距离排序数据

来自分类Dev

MATLAB:如何按索引矩阵设置矩阵?

来自分类Dev

如何从 Matlab kmean() 函数的输出计算与簇质心的距离