Comparing two structures and find the common

84 次查看(过去 30 天)
I need to compare two strctures and find the common between them and then keep the common fields as the new structure.
  4 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2019-3-10
structure_1={'field1','field2','field3'};
structure_2={'field1','field4','field3'};
structure_1=intersect(structure_1,structure_2)
madhan ravi
madhan ravi 2019-3-10
Sir Walter has given the answer already before 24mins..

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2019-3-10
common_fields = intersect( fieldnames(structure1), fieldnames(structure2));
for fn = common_fields
this = fn{1};
new_struct(1).(this) = structure1.(this);
new_struct(2).(this) = structure2.(this);
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by