Does matlab create pointers to objects or create new copies?
7 次查看(过去 30 天)
显示 更早的评论
I'm building a simple grid simulation, where my class
contains
. I want each
to be able to keep track of which grid it is in, but I don't want to create 10,000 copies of the same
for a 100 x 100 grid. Consider the following
made up of
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/304963/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/304939/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/304943/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/304947/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/304951/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/304955/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/304959/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/304963/image.png)
myGrid = cgrid(30, 20)
myGrid =
cgrid with properties:
grid: {30×20 cell}
M: 30
N: 20
Now suppose I want each cell to know which grid it is in, so I can write functions for
that extract useful information from the grid (i.e., findNeighbors)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/304967/image.png)
%Snippet of cgrid constructor, modified for clarity
for ii = 1:myGrid.M
for jj = 1:myGrid.N
obj.grid{ii, jj} = ccell(ii, jj); %Creates a ccell object and stores it at the location {ii, jj}
obj.grid{ii, jj}.home = myGrid.grid %Tells the ccell object where it lives
end
end
Does this second line of the nested for loop create 600 copies of myGrid? Or does it just point to where myGrid is stored?
Because if I were to take any cell and modify its home, that wouldn't affect my original object of myGrid. It would seem so wasteful for a new object to be created everytime, but I know that somewhere somehow, obj.grid{1,2}.home is NOT the same object as myGrid.grid
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!