MATLAB 中的浅拷贝行为

Shallow copy behavior in MATLAB

很多人在开始使用 Python 时会遇到意想不到的浅拷贝行为,我想确保我不会在 MATLAB 中犯任何这些错误(我用它较少经验)。

我读了这篇关于 object behaviors in MATLAB and I read this question that pertains to pointer/handle behavior. Are there any situations where isa(obj, 'handle') would return false but you would still encounter the situation where modification of obj would result in the modification of another variable (to my knowledge, any argument modification by a function call should trigger a copy on write and duplicate the variable in memory) 的文章?

这是对 MATLAB 中类似 "shallow copy" 行为的完整理解吗?标准值对象复制行为是否有任何其他注意事项?

值 class 可以包含句柄 class,如果您修改它,您将更改句柄 class 的实例。例如(注意 containers.Map 是一个内置的 class ,它是一个句柄 - 没什么特别的,我只是为了方便而选择它:

>> a = containers.Map; a('hello') = 1;
>> b = struct('field1', 1, 'field2', a);
>> isa(b, 'handle')
ans =
  logical
   0
>> b.field2('hello') = 2;
>> a('hello')
ans =
     2

所以 b 是一个结构(具有值语义),但它的一个字段包含一个 containers.Map,它是一个句柄并具有引用语义。当您修改该字段时,您也会更改 a,这是对基础 containers.Map.

的另一个引用