【问题标题】:union of two matrices with their common attribute added together in matlab两个矩阵的并集,它们的共同属性在matlab中加在一起
【发布时间】:2017-04-28 16:19:33
【问题描述】:

我有两个结构,我希望有两个矩阵的并集,而第三行是根据共同的前两行添加的,结果对于前两行是顺序不敏感的,并且避免了重复值.即

% struct1
row1 = [1  1  1  2  4  3];
col1 = [1  2  3  1  2  4];
att1 = [2  3  4  6  2  5];
% struct2
row2 = [2  2  1  3  3];
col2 = [1  2  3  1  4];
att2 = [1  0  1  1  5];
% result
resultRow = [1 1 1 2 2 3]
resultCol = [1 2 3 2 4 4]
resultAtt = [2 10 6 0 2 10]

我之前问过the intersection of two structure,但似乎 accumarray 适用于行而不是矩阵。任何帮助表示赞赏。

【问题讨论】:

  • 这两组输入向量是如何生成结果向量的,完全不清楚。
  • 结果在内部排序,为结果行和列选择每行和列之间较小的值,并添加它们的属性值。 idx1 = sort([row1(:) col1(:)],2); idx2 = sort([row2(:) col2(:)],2); union(idx1,idx2,'rows') 给出了前两行结果的结果,如链接问题中所述。
  • 那么与链接问题有什么不同?
  • 链接的问题是关于交集的,这个问题是关于联合的。我正在努力寻找工会案的答案。

标签: matlab matrix union


【解决方案1】:

基于这个问题的intersection部分的解决方案,我认为我们可以进行如下操作:

% struct1
row1 = [1  1  1  2  4  3];
col1 = [1  2  3  1  2  4];
att1 = [2  3  4  6  2  5];
% struct2
row2 = [2  2  1  3  3];
col2 = [1  2  3  1  4];
att2 = [1  0  1  1  5];
% sort in 2nd dimension to get row-column indexes insensitive for order
idx1 = sort([row1(:) col1(:)],2);
idx2 = sort([row2(:) col2(:)],2);
%find union
[idx,~,bins] = unique([idx1;idx2],'rows','stable');
att = accumarray(bins,[att1,att2]);
ResultUnion= [idx att]';
disp(ResultUnion)

你得到

ResultUnion =

     1     1     1     2     3     2
     1     2     3     4     4     2
     2    10     6     2    10     0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多