使用花括号和圆括号访问单元格元素的区别
使用圆括号 ()
和花括号 {}
访问元胞数组中的元素有什么区别?
What is the difference between accessing elements in a cell array using parentheses ()
and curly braces {}
?
例如,我尝试使用 cell{4} = []
和 cell(4) = []
.在第一种情况下,它将第 4 个th 元素设置为 []
,但在第二种情况下,它清除了单元格元素,即将单元格元素计数减少了 1.
For example, I tried to use cell{4} = []
and cell(4) = []
. In the first case it sets the 4th element to []
, but in the second case it wiped out the cell element, that is, reduced the cell element count by 1.
把元胞数组想象成一个普通的同构数组,它的元素都是 cell
s.括号 (()
) 只是访问 cell
包装对象,而使用大括号 ({}
) 访问元素给出包含在单元格.
Think of cell array as a regular homogenic array, whose elements are all cell
s. Parentheses (()
) simply access the cell
wrapper object, while accessing elements using curly bracers ({}
) gives the actual object contained within the cell.
例如
A={ [5,6], 0 , 0 ,0 };
看起来像这样:
使元素等于 []
带括号的语法实际上是删除该元素的请求,因此当您要求执行 foo(i) = []
删除 i-th 单元格.它不是一个赋值操作,而是一个 RemoveElement
操作,它使用类似于赋值的语法.
The syntax of making an element equal to []
with parentheses is actually a request to delete that element, so when you ask to do foo(i) = []
you remove the i-th cell. It is not an assignment operation, but rather a RemoveElement
operation, which uses similar syntax to assignment.
但是,当您执行 foo{i} = []
时,您正在为第 i 个单元格分配一个新值(这是一个空数组),从而清除该单元格的内容.
However, when you do foo{i} = []
you are assigning to the i-th cell a new value (which is an empty array), thus clearing the contents of that cell.