Borrar filtros
Borrar filtros

Problems in running for loop on keys on container maps

6 visualizaciones (últimos 30 días)
Tsz Tsun
Tsz Tsun el 19 de Ag. de 2022
Comentada: Tsz Tsun el 19 de Ag. de 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.

Respuesta aceptada

Bruno Luong
Bruno Luong el 19 de Ag. de 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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by