How to make an array of pointers to a subset of values in an existing array?

7 次查看(过去 30 天)
I'm writing a simulation where ClassA is a space inside of which a number of ClassB objects exist. ClassA keeps track of interaction parameters between all of the ClassB objects in large arrays, but the ClassB objects use that information to update themselves,. Depending on some other conditions they don't always update and/or have some unique behavior. This is all to say that I would like a couple of big arrays of information to exist in A, but relevant subsets of that information to be accessible to each B object. So far I've been handling this by storing copies of these relevant subsets in each ClassB object and updating them whenever ClassA updates, but as I have more B objects this is starting to generate a problematic amount of overhead.
So the question: Is there any way to have, say, an nxm array of values in ClassA, and then 1xm arrays of pointers in each of n ClassB objects corresponding to rows of the the nxm ClassA array? I am aware I could make a ClassC object, which inherits handle, and use an array of n such objects in ClassA in lieu of the nxm array itself, then pass relevant handles to ClassB objects, but is there a more direct way to generate pointers to subsets of existing data?

采纳的回答

Guillaume
Guillaume 2018-11-24
No, it's not possible at all. Matlab does not have pointers and there is no mechanism to access slices of a matrix (even read-only) that does not involve making a copy of that slice.
You could indeed create a separate handle class that would store the rows of the matrix but you don't need to do that as long as B doesn't need to modify the array in A. Instead you could store your rows in a cell array in A. You can then pass (by value) these rows to B. As long as B does not modify these row, due to the copy-on-write mechanism of matlab, you'll be in effect passing pointers to the rows. However, as soon as B tries to modify a row, you'll initiate a copy of that row. In fact, if B doesn't modify the array in A, you could just pass the whole array to each B with no memory overhead. Actual copying only occurs when the copy is modified.
If B does need to modify the array, then you'd have to go the way of your separate C class. That's going to create a fair bit of overhead. Personally,instead I'd create an interface in A only accessible to B so that B can query/update the array in A whenever it needs.
  1 个评论
Grant Junno
Grant Junno 2018-11-24
Thank you! I am still learning the ways in which MATLAB can and can't and can kind of do pointers. I actually didn't realize it was the fact I was modifying things in ClassB and updating the original array from there that was causing the overhead (the copy-on-write mechanism). That's nifty, and I think I can work with that alone, though the interface idea is a good one as well.
Thanks again.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by