Reputation: 6503
I have two container.Map objects, with identical keys and values. Is there a Matlab function, that is going to return true in the following scenario:
>> m1 = containers.Map('hi', 'ho');
>> m2 = containers.Map('hi', 'ho');
>> m1 == m2
ans =
0
Upvotes: 3
Views: 968
Reputation: 3587
isequal
is your friend here...
From the help for 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.
and as mentioned by @gire containers.Map
is linked to the handle
class
So with the simple maps given
isequal(m1,m2)
ans =
1
Upvotes: 1
Reputation: 1105
The containers.Map
class inherits from the handle class which means that the ==
operator will only return true in the following case:
m1 = containers.Map('hi', 'ho');
m2 = m1;
m2 == m1
Handles behave like a pointer (to some extent!).
If you want to compare two different maps you need to loop their elements and compare one by one. For example:
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
Upvotes: 1