필터 지우기
필터 지우기

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

조회 수: 11 (최근 30일)
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

채택된 답변

Adam Danz
Adam Danz 2019년 12월 4일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by