Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object array efficiency in Matlab

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.

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:

% Point.m
classmethod Point < 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

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.

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:

  1. 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?
  2. How does this affect operations like [points.x], something that I would need to do quite often? Is this considered an efficient operation?
  3. Is there a better way to initialize the object array? The above loop construction seems highly inefficient.
  4. I suppose it should be possible to simulate object-like behaviour while storing properties more favourably, perhaps by overloading subsref. Would you recommend that?

Or to put things more general: what would be the optimal way of organizing my points?

Looking forward to your advice!

like image 452
gertjan Avatar asked Sep 30 '10 09:09

gertjan


People also ask

Are Matlab cell arrays slow?

I've found that the performance of cell Arrays, multi-dimensional arrays, and structure arrays are all far slower than the use of multiple independent variables. Thus, my optimized codes have been using super long if-statements and functions which completely write out which variables are needed or not.

How do you predefine the size of an array in Matlab?

sz = size( A ) returns a row vector whose elements are the lengths of the corresponding dimensions of A . For example, if A is a 3-by-4 matrix, then size(A) returns the vector [3 4] .

What is a cell array in Matlab?

A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of text, combinations of text and numbers, or numeric arrays of different sizes.

How do you create an array of classes in Matlab?

A class constructor can create an array by building the array and returning it as the output argument. For example, the ObjectArray class creates an object array that is the same size as the input array. Then it initializes the Value property of each object to the corresponding input array value.


1 Answers

Not really answering your questions in order, but here's some hopefully useful information:

  1. Objects are stored in memory in the same way as structures - each field is its own fully-fledged MATLAB array (mxArray to C-Mex programmers), so the size of each field can be independent.
  2. I would probably make something like a single PointList object with fields x, y, prop1, prop2. These fields would then be vectors of the appropriate length. This will almost certainly be more efficient than a list of Point objects. It will certainly take less memory.
  3. You should define accessor methods on PointList to ensure that your vectors are always the same lengths etc.
  4. If you really wanted to, you could have your PointList have a "capacity" that is larger than the number of elements currently stored in it - that way, you could avoid resizing x, y, ... all the time
  5. In general, overloading subsref is not for the faint-hearted. Bear in mind that you also need to correctly overload at least numel, ndims, length, end and size too.
like image 53
Edric Avatar answered Sep 19 '22 12:09

Edric