Problems in running for loop on keys on container maps

6 次查看(过去 30 天)
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.

采纳的回答

Bruno Luong
Bruno Luong 2022-8-19
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by