File Exchange

image thumbnail

A multidimensional map class

version 1.1 (9.19 KB) by

MapN provides a map class similar to containers.Map, but with multidimensional keys.



View License

MapN provides a multidimensional map container.

A MapN object contains key lists and values. A value is some unit of data that you want stored in the MapN object, and a key list is a unique reference to that data.

MapN is modelled on containers.Map, but whereas a containers.Map object is indexed by a single scalar or string key, a MapN object is indexed by an ordered set of such keys, expressed as a comma-separated list of arguments.

A MapN object is constructed like this:

M = MapN();

Values are stored using M(key1, key2, ...) = value, for example:

M(1, 'a') = 'a string value';
M(1, 'b') = 287.2;
M(2) = [1 2 3; 4 5 6];
M(2, 'x', pi) = {'a' 'cell' 'array'};

and values are retrieved using M(key1, key2, ...), for example

v = M(1, 'b');
u = M(2);

This contribution also includes a memoize function, inspired by the blog entry at The memoize function demonstrates one use of MapN.

Comments and Ratings (9)

David Young

David Young (view profile)

Thank you Renwen Lin, I'll look at that suggestions.

Renwen Lin

SUGGESTION: New feature like:
VREST = values(M,{'KEY1' 'KEY2'})
M({'KEY1' 'KEY2'})


Xiaohu (view profile)

Double checked that the bug is fixed. Thanks a lot again David!

David Young

David Young (view profile)

Xiaohu - bug now fixed.

M I - Thank you for the suggestion but I have not adopted it. There are discussions of the reasons here: and here

David Young

David Young (view profile)

Xiaohu - thank you. I will have a look at the problem you report shortly.


Xiaohu (view profile)

Really nice code! Thank you very much for the submission.

Just want to provide a feedback. I think there is a bug on remove(), which can be reproduced with the code below. The result should be a MapN object with one item, however it returns no keys. Please let me know if I'm using it wrong.

Thanks again.

m = MapN();
m(1, 'a') = 'x';
m(2, 'b') = 222;
remove(m, 1, 'a');

David Young

David Young (view profile)

Thanks M I. Looks like that should go in, and I'll try to get round to it soon.


M I (view profile)

very nice.
a little Update to your Code:
you can also use function calls like "map.isKey(..)" and avoid the according error, if you add the following at beginning of the function "subsref(M, S)":

if ~strcmp(S(1).type, '()')
v = builtin('subsref', M, S);


Andrew (view profile)

Very useful! Wish Matlab had this functionality built in. Would be nice from a runtime standpoint though if accessing/modifying could be done in more of a vectorized fashion.



Fixed bug in remove method. My thanks to Xiaohu for reporting the bug.

MATLAB Release
MATLAB 8.2 (R2013b)

Inspired: davestanley/MultiDimensionalDictionary

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video