我有两个container.Map对象,具有相同的键和值。是否有Matlab函数,在以下场景中将返回true:
>> m1 = containers.Map('hi', 'ho');
>> m2 = containers.Map('hi', 'ho');
>> m1 == m2
ans =
0
答案 0 :(得分:1)
containers.Map
类继承自handle类,这意味着==
运算符只会在以下情况下返回true:
m1 = containers.Map('hi', 'ho');
m2 = m1;
m2 == m1
句柄表现得像指针(在某种程度上!)。
如果你想比较两个不同的地图,你需要循环它们的元素并逐个比较。例如:
keys1 = m1.keys;
keys2 = m2.keys;
% // If the keys are not equal there is no reason to loop
if ~isequal(keys1, keys2)
disp('Maps are not equal');
return;
end
% // Since at this point it is known that keys1 == keys2, keys1 is used as a
% // base
results = false(size(keys1));
for i = 1:length(keys1)
results(i) = ms1(keys{i}) == ms2(keys{i});
end
if all(results)
disp(';aps are equal');
else
disp('Maps are not equal');
end
答案 1 :(得分:0)
isequal
是你的朋友......
来自isequal
的帮助
When comparing handle objects, use EQ or the == operator to test whether objects are the same handle. Use isequal to test if objects have equal property values, even if those objects are different handles.
并且@gire containers.Map
提到的链接到handle
类
所以给出了简单的地图
isequal(m1,m2)
ans =
1