Problems in running for loop on keys on container maps

2 views (last 30 days)
Hi all, I am very new to matlab and I have some problems in using container map. I have a ketSet with grid index 1,1 1,2 1,3 and I would like to run a for loop on that to do some dot product. My question is in the for loop p = 1:2, how can I cast the index into the numerical key of the map?
keySet = {'1,1', '1,2', '1,3'};
%use cell array
valueSet = {[2 3], [3 4], [9,6]};
M = containers.Map(keySet,valueSet);
for p=1:2
dot(M('1,p'),M('1,p+1'))
end
Error using indexing
The specified key is not present in this container.

Accepted Answer

Bruno Luong
Bruno Luong on 19 Aug 2022
keySet = {'1,1', '1,2', '1,3'};
%use cell array
valueSet = {[2 3], [3 4], [9,6]};
M = containers.Map(keySet,valueSet);
for p=1:2
dot(M(sprintf('1,%d',p)),M(sprintf('1,%d',p+1)))
end
ans = 18
ans = 51

More Answers (0)

Community Treasure Hunt

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

Start Hunting!