tags:

views:

252

answers:

3

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.

+2  A: 

the following link might be of help:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brd4btr.html#brd4nrh

Waleed Al-Balooshi
Thanks, it was massively helpful. I added ant = TAnt.empty(antsNumber,0) and it works!
Kamran
According to the link, the answer is ant = TAnt.empty(antsNumber, 0)
Kamran
@Kamran: Your solution using the `empty` method of an object may work in the sense that it makes MATLAB stop yelling at you about preallocation, but it isn't really preallocation because it is still an *empty* array. Take note of this statement in the documentation Waleed linked to, under the heading "Assigning Values to an Empty Array": "If you make an assignment to a property value, MATLAB calls the SimpleClass constructor to grow the array to the required size." Note the word **grow**, indicating that the array changes size when assigned to, which is what preallocation is meant to avoid.
gnovice
@Kamran: as pointed out by gnovice, using the static method `empty` does not really perform preallocation. Instead you should add the following line before the for loop: `ant(antsNumber) = TAnt(source, target);` provided you have a default constructor with nargin=0 (which is called for the elements in `ant(1:antsNumber-1)`)
Amro
A: 

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.

Leif Andersen
It is totally wrong. MATLAB error: Conversion to double from TAnt is not possible.
Kamran
+2  A: 

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.

gnovice
it virtually works but before that it should be modified to ant = repmat(TAnt(source,target),0,5)
Kamran
@Kamran: That would give you an empty array (0-by-5). For preallocation, you really want to create a default array of the *same size* as what you will end up with (1-by-5).
gnovice
It seems so but the "(0,5)" works! when I try to use (1,5) it causes an error.
Kamran
@gnovice: none of them works... it says: Expression or statement is incomplete or incorrect.
Kamran
@Kamran: Since you haven't given any details about your `TAnt` class, I would have to guess that your errors are due to the fact that you didn't design your class constructor to handle a no input argument case. You must do this for these solutions to work. See this link: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brd2m9e-1.html#bripq7u-1
gnovice