Borrar filtros
Borrar filtros

In Matlab can I directly modify a value in a containers.map when the value is the element of an array?

11 visualizaciones (últimos 30 días)
I'm wondering if this syntax is possible? Specifically, I want to increment the first element of the array which is the number 3, cooresponding to the first value that the key 'Li' points to. Specifically, I'm asking about the line "M('Li')(i) = M('Li')(i) + 1"
keySet = {'Li','Jones','Sanchez'};
testLi = [3 4 5];
testJones = [27 3.92 6.4 8.21];
testSanchez = 'C:\Tests\Sanchez.dat';
valueSet = {testLi,testJones,testSanchez};
M = containers.Map(keySet,valueSet,'UniformValues',false)
M('Li')(i) = M('Li')(i) + 1
I know this doesn't work, it throws an error. Can this be done without code that looks like this:
hold = M('Li')
hold(1) = hold(1) + 1
M('Li') = hold

Respuesta aceptada

Adam Danz
Adam Danz el 4 de Dic. de 2019
You could add a local function at the bottom of the m-file that does the dirty work.
M = plusOne(M,'Li',i);
function mapObj = plusOne(mapObj,key,idx)
% This function merely adds 1 to mapObj(key)(idx)
% INPUTS
% mapObj is the handle to the map container
% key is a char array naming the key
% idx is a positive integer identifying which value to add one to
v = zeros(size(mapObj(key)));
v(idx) = 1;
mapObj(key) = mapObj(key) + v;
end % 'end' may or may not be needed

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by