Reputation: 30704
>> 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]
Note that A1
and A2
display differently.
A1
and A2
report as being the same class and dimension:
>> info = @(x){class(x),size(x)};
>> info(A1)
ans =
'cell' [1x2 double]
>> info(A2)
ans =
'cell' [1x2 double]
yet they and are not considered equal:
>> isequal( A1, A2 )
ans =
0
However, A1{:}
is considered as being equal to A2
:
>> isequal( A1{:}, A2 )
ans =
1
Could someone explain what is going on here?
Upvotes: 1
Views: 147
Reputation: 18177
()
is used for function inputs and to index arrays; []
is used to denote arrays; {}
is used to index cells.
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
So in conclusion: x(1)
selects the first element of array x
or evaluates the function x
at 1
; x[1]
should not be possible, as square brackets can't be used to index stuff; x{1}
would select the first cell in cell array x
.
As to your specific problem:
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
Finally A1{:}
gets the content of the cell out of the cell, ready to store as a separate variable, hence it is equal to A2
, which already contains that content. (A1{:}
is a comma separated list, thanks to rayryeng for pointing that out.)
Upvotes: 4
Reputation: 1693
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
Upvotes: 2
Reputation: 450
data{1} is to access the content of a cell data(1) is to create a cell with a copy of the first entry of data. This is the same as :
>> A1=cell(1,1);
>> A1{1}=data{1};
In your example A1 is thus a cell containing a cell, while A2 is a cell with three entries
Upvotes: 2