Arrays 根据matlab中数组中行之间的公共值排列行

Arrays 根据matlab中数组中行之间的公共值排列行,arrays,matlab,scripting,Arrays,Matlab,Scripting,我有一个800行3列的数组。我想以这样的方式安排它:第一行之后的行包含至少2个公共值作为第一行,第二行之后的下一行也包含至少2个公共值作为第二行。 榜样第1行=2 4 5普通行=第30行=2 5 13 所以第一个安排 2 4 5 2 5 13 下一行将是新行2的公用行 示例第4行=13 45 5 因此,现在的安排将是: 2 4 5 2 5 13 13 45 5 etc 目前,我有一段代码,它将这些常见的元素组合在单元格中,然后一个接一个地显示

我有一个800行3列的数组。我想以这样的方式安排它:第一行之后的行包含至少2个公共值作为第一行,第二行之后的下一行也包含至少2个公共值作为第二行。 榜样<代码>第1行=2 4 5普通行=
第30行=2 5 13
所以第一个安排

 2  4  5
 2  5 13 
下一行将是新行2的公用行
示例
第4行=13 45 5
因此,现在的安排将是:

2   4   5
2   5   13
13  45  5    

    etc
目前,我有一段代码,它将这些常见的元素组合在单元格中,然后一个接一个地显示出来。问题是数组包含多个公共项。。例如,第1行可能有两行,这两行具有相同的公共值,此代码将所有这些行合并到一个数组中,并对第二行执行相同的操作。。我怎样做代码我让代码按照我在第一段中解释的那样做

这是代码

% Data
A = connections;
% Engine
[m, n] = size(A);
groups =[];

ng = 0;
for k=1:m-1
    u = unique(A(k,:)); % representation of kth row
    [in, J] = ismember(A(k:end,:),u);
    l = m-k+1;
    r = repmat((1:l).', n, 1);
    c = accumarray([r(in) J(in)],1,[l n]); % count
    c = bsxfun(@min,c,c(1,:)); % clip
    rows = sum(c,2)>=2; % check if at least 2 elements are common
    rows(1) = false;
    if any(rows)
        ng = ng+1;
        rows(1) = true;
        groups = (k-1) + find(rows);

    end
end
请注意,只有当
groups
是一个单元数组时,才能添加下面的其余代码,但它已被更改为上面所述的正常数组。。因此,为了测试代码,不需要添加下面的代码

% Remove the tail
groups(ng+1:end) = [];
% Display
for k=1:1:length(groups)
    gk = groups{k};
    for r = gk'
     fprintf('%d %d %d %d\n', r, A(r,:));        

    end
end
这是最新的。或可跟踪路径是一条恰好访问每个顶点一次的路径。这里有800个顶点(行),每个顶点都有一个或多个邻居。邻域的条件是一个顶点具有与其他顶点相同的2个或更多值。 要解决这个问题,您应该创建一个对应于图结构的邻接矩阵,然后在图上找到哈密顿路径

以下是创建邻接矩阵的方法:

[ m n] = size(A);
%generate all 2 element subsets of the set [1 2 3 ] to check if a row
% has two elemnts common with other row
x = nchoosek(1:n,2);
%generate indices for generating two element rows
col = repmat(x,m,1);
M = (1:m).';
row = kron(M,ones(size(x)));
IDX = sub2ind([m n] ,row, col);
%convert A with [m , 3] to a [(m * 3), 2)]matrix 
% then sort each row to be comparable with other rows
B=sort(A(IDX),2);
%indices of rows that have two common elements is found
[~,I,II] = unique(B,'rows', 'first');
convert index of the [(m * 3), 2)]matrix to [m , 3] matrix
I = uint32(I-1) / n + 1;
%create adjacency matrix
adjacency= sparse(I(II(:)), M(II(:)),1,m,m);
%make the matrix symmetric
adjacency = adjacency | adjacency.';
% a vertex can not be neighbor of itself
adjacency(speye(m))=false;
adjacency = [adjacency; ones(1,m)];
adjacency = [adjacency, ones(n+1,1)];
adjacency (end) = 0;
您应该实现自己版本的哈密顿路径算法或在web上查找。
然而,哈密顿路径问题可以通过向图中添加一个顶点并使所有其他顶点与之相邻,转化为一种特殊形式的

因此,应对邻接矩阵进行一些更改:

[ m n] = size(A);
%generate all 2 element subsets of the set [1 2 3 ] to check if a row
% has two elemnts common with other row
x = nchoosek(1:n,2);
%generate indices for generating two element rows
col = repmat(x,m,1);
M = (1:m).';
row = kron(M,ones(size(x)));
IDX = sub2ind([m n] ,row, col);
%convert A with [m , 3] to a [(m * 3), 2)]matrix 
% then sort each row to be comparable with other rows
B=sort(A(IDX),2);
%indices of rows that have two common elements is found
[~,I,II] = unique(B,'rows', 'first');
convert index of the [(m * 3), 2)]matrix to [m , 3] matrix
I = uint32(I-1) / n + 1;
%create adjacency matrix
adjacency= sparse(I(II(:)), M(II(:)),1,m,m);
%make the matrix symmetric
adjacency = adjacency | adjacency.';
% a vertex can not be neighbor of itself
adjacency(speye(m))=false;
adjacency = [adjacency; ones(1,m)];
adjacency = [adjacency, ones(n+1,1)];
adjacency (end) = 0;

在中,您可以找到一个示例,说明如何使用
二进制整数规划解决旅行推销员问题。然而,提供的方法似乎并不简单。因此,您可以找到另一个或实现您自己的。

我现在刚刚尝试了它,它没有给出任何错误。。它在哪里说它在什么是
连接时出错?什么是
单元格
连接
是一个包含800行和3列的数组,它在每行中最多应有2个与数组中另一行相似的值。。我已将
单元格
部分更改为一个空矩阵,稍后将填充。这对我来说非常混乱,非常复杂。虽然我在我的项目上有了一些进展。