Matlab 中的对象数组效率
对于我的工作,我必须在 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
我更喜欢上述的原因既美观(对象应该是对象)又实用,因为它允许我轻松创建点的子集,而无需索引多个不同的数组。
然而,我想知道这是否是最有效的做事方式,考虑到点集可能会增长到数千或数万点的数量级。我的主要问题是:
- 根据我的理解:Matlab 如何在内存中存储对象数组?它如何处理依赖于 prop1 的不同对象大小(例如,结构体)?
- 这对像 [points.x] 这样我需要经常执行的操作有何影响?这是否被认为是一种有效的操作?
- 有没有更好的方法来初始化对象数组?上面的循环结构看起来效率很低。
- 我认为应该可以模拟类似对象的行为,同时更有利地存储属性,也许可以通过重载
或者更笼统地说:组织我的观点的最佳方式是什么?
期待您的建议!
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:
- 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?
- How does this affect operations like [points.x], something that I would need to do quite often? Is this considered an efficient operation?
- Is there a better way to initialize the object array? The above loop construction seems highly inefficient.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并没有真正按顺序回答您的问题,但这里有一些希望有用的信息:
Not really answering your questions in order, but here's some hopefully useful information: