>> x = { {'a',[1],[2]}; {'b',[3],[4]} }
x =
{1x3 cell}
{1x3 cell}
>> A1 = x(1)
A1 =
{1x3 cell}
>> A2 = x{1}
A2 =
'a' [1] [2]
请注意,A1
和A2
的显示方式不同。
A1
和A2
报告为同一类和维度:
>> info = @(x){class(x),size(x)};
>> info(A1)
ans =
'cell' [1x2 double]
>> info(A2)
ans =
'cell' [1x2 double]
然而他们并不被认为是平等的:
>> isequal( A1, A2 )
ans =
0
但是,A1{:}
被视为等于A2
:
>> isequal( A1{:}, A2 )
ans =
1
有人能解释一下这里发生了什么吗?
答案 0 :(得分:4)
()
用于函数输入和索引数组; []
用于表示数组; {}
用于索引单元格。
A = [2 1]; % Creates and array A with elements 2 and 1
A(2) =2; % Sets the second element of A to 2
B = cell(1,2); % creates a cell array
B{2} = A; % Stores A in the second cell of B
总之:x(1)
选择数组x
的第一个元素或评估x
处的函数1
; x[1]
不可能,因为方括号不能用于索引内容; x{1}
将选择单元格数组x
中的第一个单元格。
关于你的具体问题:
A1 = x(1); % makes a copy of the element on index one, being a 1x3 cell
A2 = x{1}; % stores the content of the cell at element 1 in A2
最后A1{:}
从单元格中获取单元格的内容,准备作为单独的变量存储,因此它等于A2
,它已经包含该内容。 (A1{:}
是comma separated list,感谢rayryeng指出这一点。)
答案 1 :(得分:2)
data {1}用于访问单元格的内容 data(1)用于创建具有第一个数据条目副本的单元格。这与:
相同rake db:migrate
在您的示例中,A1因此是包含单元格的单元格,而A2是具有三个条目的单元格
答案 2 :(得分:2)
Your variable x
is a cell containing 2 (sub)cells.
Curly brackes is used to get the content of a cell, thus,
A2 = x{1}
ans =
'a' [1] [2]
Which gives the same result as
A2 = {'a',[1],[2]}
Parentheses is used for indexing and therefore returns a subset of the (sub)cells
A1 = x(1)
A1 =
{1x3 cell}
Which gives the same result as
A1 = { {'a',[1],[2]} }
Your anonymous info function returns a cell (needs to as the content is of mixed types). The result
'cell' [1x2 double]
tells that both A1 and A2 is cells which is true, not considering the fact that A1 is a cell containing cells and A2 is a cell containing a char and 2 numbers.
The [1x2 double]
simply tells that the answer from size is itself of size [1x2]. This is true as long as you don't use higher dimensions than 2.
If you dig deeper into your info
answer you will see the real size:
A1info = info(A1);
A2info = info(A2);
A1info{2}
ans =
1 1
A2info{2}
ans =
1 3
The part of isequal( A1, A2 )
beeing false
I think you understand by now.
Also, that isequal( A1{:}, A2)
is true, since {:} "de-cell" A1
and pick out its content.
In this case, you could have typed isequal( A1{1}, A2)
as well