使用花括号和括号访问单元格元素之间的区别 [英] Difference between accessing cell elements using curly braces and parentheses
问题描述
使用括号()
和花括号{}
访问单元格数组中的元素有什么区别?
What is the difference between accessing elements in a cell array using parentheses ()
and curly braces {}
?
例如,我尝试使用cell{4} = []
和cell(4) = []
.在第一种情况下,它将第4个 元素设置为[]
,但是在第二种情况下,它擦除了单元元素,即将单元元素数减少了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
.括号(()
)仅访问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 第em个单元格.它不是赋值操作,而是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.
这篇关于使用花括号和括号访问单元格元素之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!