Note that there are some explanatory texts on larger screens.

plurals
  1. POobject array efficiency in Matlab
    primarykey
    data
    text
    <p>For my work I have to set up a project in Matlab, which is not my language of choice and I have some questions regarding efficiency.</p> <p>I am currently dealing with a collection of points with several properties. Rather than putting all of these in separate arrays of equal length I would much prefer to make a single array of Point objects, using Matlab's user defined classes. For example:</p> <pre><code>% Point.m classmethod Point &lt; handle properties x, y, prop1, prop2 end end % script.m ... % define x(100), y(100), prop1(100), prop2(100) points(100) = Point; % this seems to be the way to allocate an object vector for i = 1:100 points(i).x = x(i); % copy values into object points(i).y = y(i); points(i).prop1 = prop1(i); points(i).prop2 = prop2(i); end </code></pre> <p>The reason that I prefer the above is both aesthetic (objects should be objects) and practical, as it allows me to easily create subsets of points without having to index several different arrays.</p> <p>However I wonder if it is the most efficient way of doing things, considering that the set of points might grow quite large in the order of thousands or tens of thousands of points. My main questions are:</p> <ol> <li>For my understanding: how does Matlab store object arrays in memory? How does it handle varying object size dependent of prop1 being, for instance, a struct?</li> <li>How does this affect operations like [points.x], something that I would need to do quite often? Is this considered an efficient operation?</li> <li>Is there a better way to initialize the object array? The above loop construction seems highly inefficient.</li> <li>I suppose it should be possible to simulate object-like behaviour while storing properties more favourably, perhaps by overloading <a href="http://www.mathworks.es/help/techdoc/ref/subsref.html" rel="noreferrer">subsref</a>. Would you recommend that?</li> </ol> <p>Or to put things more general: what would be the optimal way of organizing my points?</p> <p>Looking forward to your advice!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload