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

7 views (last 30 days)
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?

Accepted Answer

Guillaume
Guillaume on 24 Nov 2018
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 Comment
Grant Junno
Grant Junno on 24 Nov 2018
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!