Code covered by the BSD License  

Highlights from
A multidimensional map class

4.66667

4.7 | 4 ratings Rate this file 23 Downloads (last 30 days) File Size: 9.19 KB File ID: #33068

A multidimensional map class

by

 

29 Sep 2011 (Updated )

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

| Watch this File

File Information
Description

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 http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/ The memoize function demonstrates one use of MapN.

MATLAB release MATLAB 8.2 (R2013b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
01 Jul 2014 Xiaohu

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

28 Jun 2014 David Young

Xiaohu - bug now fixed.

M I - Thank you for the suggestion but I have not adopted it. There are discussions of the reasons here: http://www.mathworks.co.uk/matlabcentral/answers/16652-overriding-subsref-and-subsasgn-effect-on-private-properties and here http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/319995

15 Apr 2014 David Young

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

04 Apr 2014 Xiaohu

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;
keys(m)
remove(m, 1, 'a');
keys(m)

12 Mar 2014 David Young

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

12 Mar 2014 M I

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);
return;
end

08 Oct 2013 Andrew

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.

Updates
30 Jun 2014

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

Contact us