How to preallocate an array of class in MATLAB?

后端 未结 4 1888
时光取名叫无心
时光取名叫无心 2020-11-30 13:29

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(sour         


        
相关标签:
4条回答
  • 2020-11-30 14:09

    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.

    0 讨论(0)
  • 2020-11-30 14:13

    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.

    0 讨论(0)
  • 2020-11-30 14:20

    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

    0 讨论(0)
  • 2020-11-30 14:34

    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

    0 讨论(0)
提交回复
热议问题