在matlab中将两个矩阵的共同属性加在一起的并集 [英] union of two matrices with their common attribute added together in matlab

查看:242
本文介绍了在matlab中将两个矩阵的共同属性加在一起的并集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two structure and I'd like to have union of two matrices while the third row is added according to the common first two rows, the result is order-insensitive with respect to the first tow rows and duplicate values are avoided. i.e.

% 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]

我之前曾问过交叉点 ,但似乎accumarray适用于行而不是矩阵.感谢您的帮助.

I have the previously asked the intersection of two structure but it seems accumarray works for rows not matrices. Any help is appreciated.

推荐答案

基于

Based on the solution to intersection part of this question, I think we can proceed as follows:

% 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

这篇关于在matlab中将两个矩阵的共同属性加在一起的并集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆