在 matlab 中搜索结构的平行行以查找常见项目
Search parallel rows of a structures for common items in matlab
我已经存储了
的 (row,col,val) 信息
key1: (1,1) (1,2) (1,3) (4,2) (3,4)
attribute1: 2 3 4 2 5
如下:
Structure A1:
key row1: 1 1 1 4 3
key col1: 1 2 3 2 4
attribute1: 2 3 4 2 5
同理,对于结构A2
Structure A2:
key row2: 2 2 1 3
key col2: 1 2 3 4
attribute2: 1 0 1 5
现在,我希望能够同时在结构 A1 和 A2 之间的行和列键项中搜索公共条目。这在概念上是 find [common_item, index]=intersect([row2,col2],[row1,col1])。我希望最终结果对行和列的顺序不敏感。因此,在我的示例中 (1,2) 键值等于 (2,1) 值。然后,应该将公共条目的属性值加在一起。预期结果是
Structure Result: //it recognizes (1,2) and(2,1) are the same.
key row: 2 1 3
key col: 1 3 4
attribute: 4 5 10
我应该如何进行搜索公共条目并进行一些操作? ismember
功能可以只在一行中搜索常见的项目,如果出现多次,则只计算第一个。此外,正如我所说,我希望它在键值中不区分顺序。
感谢您的帮助。
首先使用sort
实现行列顺序不敏感,接下来使用unique
处理每个结构中的行列重复,最后使用ismember
(使用'rows'
) 来查找结构之间的公共键。请注意,我为每个结构添加了一个内部复制以显示第二阶段效果:
% 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);
% search for duplicates inside each struct
[idx1,~,bins1] = unique(idx1,'rows','stable');
att1 = accumarray(bins1,att1);
[idx2,~,bins2] = unique(idx2,'rows','stable');
att2 = accumarray(bins2,att2);
% search common entries
common1 = ismember(idx1,idx2,'rows');
row = idx1(common1,1);
col = idx1(common1,2);
common2 = ismember(idx2,[row col],'rows');
% add common values
att = att1(common1) + att2(common2);
Result.row = row';
Result.col = col';
Result.attribute = att';
disp(Result)
你得到:
Result =
row: [1 1 3]
col: [2 3 4]
attribute: [10 6 10]
我已经存储了
的 (row,col,val) 信息 key1: (1,1) (1,2) (1,3) (4,2) (3,4)
attribute1: 2 3 4 2 5
如下:
Structure A1:
key row1: 1 1 1 4 3
key col1: 1 2 3 2 4
attribute1: 2 3 4 2 5
同理,对于结构A2
Structure A2:
key row2: 2 2 1 3
key col2: 1 2 3 4
attribute2: 1 0 1 5
现在,我希望能够同时在结构 A1 和 A2 之间的行和列键项中搜索公共条目。这在概念上是 find [common_item, index]=intersect([row2,col2],[row1,col1])。我希望最终结果对行和列的顺序不敏感。因此,在我的示例中 (1,2) 键值等于 (2,1) 值。然后,应该将公共条目的属性值加在一起。预期结果是
Structure Result: //it recognizes (1,2) and(2,1) are the same.
key row: 2 1 3
key col: 1 3 4
attribute: 4 5 10
我应该如何进行搜索公共条目并进行一些操作? ismember
功能可以只在一行中搜索常见的项目,如果出现多次,则只计算第一个。此外,正如我所说,我希望它在键值中不区分顺序。
感谢您的帮助。
首先使用sort
实现行列顺序不敏感,接下来使用unique
处理每个结构中的行列重复,最后使用ismember
(使用'rows'
) 来查找结构之间的公共键。请注意,我为每个结构添加了一个内部复制以显示第二阶段效果:
% 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);
% search for duplicates inside each struct
[idx1,~,bins1] = unique(idx1,'rows','stable');
att1 = accumarray(bins1,att1);
[idx2,~,bins2] = unique(idx2,'rows','stable');
att2 = accumarray(bins2,att2);
% search common entries
common1 = ismember(idx1,idx2,'rows');
row = idx1(common1,1);
col = idx1(common1,2);
common2 = ismember(idx2,[row col],'rows');
% add common values
att = att1(common1) + att2(common2);
Result.row = row';
Result.col = col';
Result.attribute = att';
disp(Result)
你得到:
Result =
row: [1 1 3]
col: [2 3 4]
attribute: [10 6 10]