Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab initialize array of objects

Tags:

arrays

oop

matlab

I am playing around with OOP in MATLAB, and I have the following constructor:

function obj = Squadron(num_fighters, num_targets, time_steps)            
    if nargin == 0
        num_targets = 100;
        time_steps = 100;
        num_fighters = 10;
    end
    obj.num_shooters = num_fighters;
    for iShooter = 1:obj.num_shooters
       a(iShooter) = Shooter(num_targets, time_steps);
    end
    obj.ShooterArray = a;
    obj.current_detections = zeros(num_fighters, num_targets);
end

That temporary variable 'a' smells terrible. Is there a better way to initialize an array of objects, I wish there was a push/pop method. I am sure there is a better way to do this.

like image 684
bonhoffer Avatar asked May 25 '11 18:05

bonhoffer


People also ask

Can array objects be initialized?

Initializing Array Of Objects In an array of objects, we have to initialize each element of array i.e. each object/object reference needs to be initialized. Different ways to initialize the array of objects: By using the constructors. By using a separate member method.

How do you initialize an empty array in MATLAB?

A = ClassName. empty( sizeVector ) returns an empty array with the specified dimensions. At least one of the dimensions must be 0. Use this syntax to define an empty array that is the same size as an existing empty array.

How do you create a zero matrix in MATLAB?

X = zeros( sz ) returns an array of zeros where size vector sz defines size(X) . For example, zeros([2 3]) returns a 2-by-3 matrix. X = zeros(___, typename ) returns an array of zeros of data type typename . For example, zeros('int8') returns a scalar, 8-bit integer 0 .


1 Answers

Looks like you are trying to create an array of handle objects (Shooters) and store it inside the property of another handle object (a Squardron). I have had a very similar problem discussion that might help you.

In short: What you are doing might not be pretty - but might be pretty good already.

When creating an array in Matlab it is usually a good Idea to do some pre-allocation to reserve memory which speeds up performance significantly.

In a normal case something like this:

a=zeros(1,1000);
for n=1:1000
    a(n)=n;
end

(here a=1:1000; would be even better)

For objects the pre-allocation works by assigning one of the objects to the very last field in the array. Matlab then fills the other fields before that with objects (handles) that it creates by calling the constructor of that object with no arguments (see Matlab help). Hence a pre-allocation for objects could look like this:

a(1,1000)=ObjectConstructor();
for n=1:1000
    a(n)=ObjectConstructor();
end

or simply

for n=1000:-1:1
    a(n)=ObjectConstructor();
end

Making sure Shooter can be called with no arguments you should be able to do something like:

for iShooter = obj.num_shooters:-1:1
   obj.ShooterArray(iShooter) = Shooter(num_targets, time_steps);
end

However, it turns out that for some reason this direct storing of an array of objects in another object's property creates very bad performance. (Probably the array pre-allocation does not work well in this case). Hence using an auxiliary variable and allocating the full array at once to the property is in this case is a good idea to increase performance.

I would try:

for iShooter = obj.num_shooters:-1:1
   a(iShooter) = Shooter(num_targets, time_steps);
end
obj.ShooterArray = a;

Again - for more detail see this discussion

like image 50
Jacob Avatar answered Oct 12 '22 22:10

Jacob