Matlab: dynamically storing objects, alternatives

2019-07-10 16:22发布

问题:

I have code in which there are many objects, where each is referenced by its own numeric ID/pointer. I wish to store these objects in some sort of structure where I can reference the objects from the structure using their numeric ID. However, the IDs are not sequential, and I don't want to allocate space for all of the non-existent IDs. This rules out simply creating an object array.

I'm currently using the containers.Map class which stores values/objects with lookup keys, but it's rather slow. Are there any alternatives?

As an example, this code would create a containers.Map object, map filled with fictional objects:

%create object storage container which uses uint32 keys and can store values of any class
map = containers.Map('KeyType','uint32','ValueType','Any');

%construct objects with ID property, and store in map
for ID = [8 230 755 67 102]
    map(ID) = example_obj(ID)
end

Is there anything that could replace the containers.Map map object in this code which wouldn't allocate space for all of the non-present IDs from 1 to 755?