Matlab container map with repeated keys

Consider the following container map
k = ["Jan" "Feb" "Jan" "Mars"]; % Notice that there are two repeated elements which is "Jan"
v = [10 20 30 15];
M = containers.Map(k,v)
% The output is
[keys(M); values(M)]
%{
{'Feb'} {'Jan'} {'Mars'}
{[ 20]} {[ 30]} {[ 15]}
%}
Notice that in the output only the last of the two repeated elements in the list k was included. What I instead would like to have is
%{
{'Feb'} {'Jan'} {'Mars'}
{[ 20]} {[ 10, 30]} {[ 15]}
%}
Is it possible?

댓글 수: 4

Adam
Adam 2019년 4월 3일
편집: Adam 2019년 4월 3일
It's possible if you add them like that (i.e. with both values at once for a single key entry of 'Jan'). So long as they value type is 'any' (which I think is the default) then you can store them like that, but you have to do the work yourself manually to add new elements, which is something I do in some cases.
i.e. search if the key exists, if not then add it as normal, if it does get the value, concatenate with the new value and re-set it.
Pontus Vikstål
Pontus Vikstål 2019년 4월 3일
편집: Pontus Vikstål 2019년 4월 3일
So I do like this you mean?
k = ["Jan" "Feb" "Mars"];
v = {[10 30] 20 15};
M = containers.Map(k,v)
Adam
Adam 2019년 4월 3일
Yes, that is how you would need to add them if you have them all upfront, which is fairly easy. It's just adding later elements to the map that is more convoluted.
Pontus Vikstål
Pontus Vikstål 2019년 4월 3일
Alright, I see!
Thanks for the help!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2019년 4월 3일

댓글:

2019년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by