Matlab 中的对象数组效率

发布于 2024-09-25 12:19:43 字数 945 浏览 2 评论 0原文

对于我的工作,我必须在 Matlab 中建立一个项目,这不是我选择的语言,并且我对效率有一些疑问。

我目前正在处理具有多个属性的点的集合。我更愿意使用 Matlab 的用户定义类来创建一个 Point 对象的单个数组,而不是将所有这些都放入相同长度的单独数组中。例如:

% 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

我更喜欢上述的原因既美观(对象应该是对象)又实用,因为它允许我轻松创建点的子集,而无需索引多个不同的数组。

然而,我想知道这是否是最有效的做事方式,考虑到点集可能会增长到数千或数万点的数量级。我的主要问题是:

  1. 根据我的理解:Matlab 如何在内存中存储对象数组?它如何处理依赖于 prop1 的不同对象大小(例如,结构体)?
  2. 这对像 [points.x] 这样我需要经常执行的操作有何影响?这是否被认为是一种有效的操作?
  3. 有没有更好的方法来初始化对象数组?上面的循环结构看起来效率很低。
  4. 我认为应该可以模拟类似对象的行为,同时更有利地存储属性,也许可以通过重载

或者更笼统地说:组织我的观点的最佳方式是什么?

期待您的建议!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2024-10-02 12:19:43

并没有真正按顺序回答您的问题,但这里有一些希望有用的信息:

  1. 对象以与结构相同的方式存储在内存中 - 每个字段都是其自己的成熟的 MATLAB 数组(对于 C-Mex 程序员来说是 mxArray),因此每个字段可以是独立的。
  2. 我可能会制作类似于带有字段 x、y、prop1、prop2 的单个 PointList 对象的东西。这些字段将是适当长度的向量。这几乎肯定比 Point 对象列表更有效。它肯定会占用更少的内存。
  3. 您应该在 PointList 上定义访问器方法,以确保您的向量始终具有相同的长度等。
  4. 如果您确实愿意,您可以让您的 PointList 具有大于当前存储在其中的元素数量的“容量” - 这样,你可以避免一直调整 x, y, ... 的大小
  5. 。 一般来说,重载 subsref 不适合胆小的人。请记住,您还需要至少正确重载 numel、ndims、length、end 和 size。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文