Filter löschen
Filter löschen

Problems in running for loop on keys on container maps

4 Ansichten (letzte 30 Tage)
Tsz Tsun
Tsz Tsun am 19 Aug. 2022
Kommentiert: Tsz Tsun am 19 Aug. 2022
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.

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by