简体   繁体   中英

How to preallocate an array of class in MATLAB?

I have an array of objects in MATLAB and I've called their constructors in a loop:

antsNumber  = 5;
for counter = 1: antsNumber
    ant(counter) = TAnt(source, target);
end

MATLAB warns me to use preallocation to speed up the process. I do know the benefits of preallocation but I don't know how to do that for objects.

Here are a few options, which require that you design the class constructor for TAnt so that it is able to handle a no input argument case :

  • You can create a default TAnt object (by calling the constructor with no input arguments) and replicate it with REPMAT to initialize your array before entering your for loop:

     ant = repmat(TAnt(),1,5); %# Replicate the default object 

    Then, you can loop over the array, overwriting each default object with a new one.

  • If your TAnt objects are all being initialized with the same data, and they are not derived from the handle class , you can create 1 object and use REPMAT to copy it:

     ant = repmat(TAnt(source,target),1,5); %# Replicate the object 

    This will allow you to avoid looping altogether.

  • If TAnt is derived from the handle class , the first option above should work fine but the second option wouldn't because it would give you 5 copies of the handle for the same object as opposed to 5 handles for distinct objects.

The following link might be of help:

http://www.mathworks.com/help/techdoc/matlab_oop/brd4btr.html#brd4nrh
Web archive of dead link

New link:
http://de.mathworks.com/help/matlab/matlab_oop/creating-object-arrays.html

The warning it gives is superfluous, unless you are doing computational heavy stuff, I would ignore it.

The reason why it's giving you the error, is because it has to find new space. Say, I give you a list of seven objects, and I tell you that you need to place them all in a row, I then go off, and give you a few more things you need to put somewhere. I then give you an eighth object and tell you to put it right after the seventh. Because you have stuff where the eighth object is, you either have to move it out of the way, or you have to move all seven objects. Matlab, is telling you it would be faster if you were to tell it beforehand that you want to put 5 things in there, rather than just giving it things one by one, having to look for a new spot each time. You can do that by adding this line to the top of your code:

ant = [1:5];

There are also other ways to do this too.

Not sure if I got your problem right, but if you want to initialize an array of your self-defined class "TAnt", here is how I would do it

  1. For TAnt's constructor method, put something like:

 function obj = TAnt(source, target) if nargin > 0 obj.mySource = source; obj.myTarget = target; else obj.mySource = defaultValue; obj.myTarget = defaultValue; end end 

Then to initialize/pre allocate an array of default TAnt objects,

 ants(1,n) = TAnt(); % n is the length of your ants array 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM