Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:39:15+00:00 2026-05-26T08:39:15+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:39:16+00:00Added an answer on May 26, 2026 at 8:39 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am playing around with OOP programming, and have hit a hurdle I could
I'm just getting the hang of OOP and have been playing around with Java
Playing around in order to learn XSLT, I have the following XML file and
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
While playing around with one-to-one associations in castle activerecord I stumbled upon the following
I have been playing around with various methods of making the Visitor pattern in
I've been playing around with Simple.Data and have run across something that I can't
I'm just playing around with some code but it seemed to have the specific
Playing around with Google Maps these days, with some directions. I have a map
Just playing around with interfaces and I have a question about something which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.