What is the difference between ( ) and { } when accessing elements of a cell array?

129 vues (au cours des 30 derniers jours)
Minh Tran
Minh Tran le 18 Sep 2018
Commenté : Minh Tran le 19 Sep 2018
There are two cell arrays that I'd like to consider:
a = {{'air'}, {'bee'}, {'cat'}} % Cell array of single-element cell arrays
b = {'air', 'bee', 'cat'} % Cell array of strings (aka. character arrays)
Access using the ( ) operator on `a` results in a cell:
>> a(1)
ans = {1 x 1 cell}
while access using ( ) operator on `b` results in something in single quotes:
>> b(1)
ans = 'air'
Access using the { } operator on `a` results in something in single quotes:
>> a{1}
ans = 'air'
and against `b` produces (with no quotes):
>> b{1}
ans = air
I have 2 questions.
1. What is the difference between the types of
{1 x 1} cell
'air'
air
that we see in the command window?
2. What do we call the ( ) access and the { } access and how does ( ) access differently from { }?

Réponses (2)

James Tursa
James Tursa le 18 Sep 2018
Modifié(e) : James Tursa le 18 Sep 2018
In general if A is a class whatever variable of some size, then A(1) will also be a class whatever variable of size 1x1. I.e., A(1) is just an element of A with the same class as A. E.g.,
If A is a double variable of size 1x3, then A(1) will be a double variable of size 1x1. If A is a cell array of size 1x3, then A(1) will be a cell array of size 1x1. If A is a struct array of size 1x3, then A(1) will be a struct array of size 1x1. Etc...
It follows then, that if A is a cell array, then A(1) is also a cell array with size 1x1.
The curly braces essentially dereference what is inside a cell array element. The result is the contents of the cells.
With that in mind:
A = {{'air'}, {'bee'}, {'cat'}} % Cell array of single-element cell arrays
A is a cell array. The elements of A happen to also be cell arrays.
A(1) is a 1x1 cell array that is the 1st element of A. That element happens to contain another cell array, namely {'air'}. The contents of that cell array is the character string 'air'. Conceptually (since the following syntax doesn't actually work in MATLAB),
A(1) is {{'air'}}
A(1){1} is {'air'}
A(1){1}{1} is 'air'
B = {'air', 'bee', 'cat'} % Cell array of strings (aka. character arrays)
B is a cell array. The elements of B happen to be character strings.
B(1) is a 1x1 cell array that is the 1st element of B. That element happens to contain the character string 'air'. Conceptually (since the following syntax doesn't actually work in MATLAB),
B(1) is {'air'}
B(1){1} is 'air'
The "conceptually" qualifier above is because you can't string together the ( ) and { } indexing like I did in MATLAB. You would need to separate that indexing into multiple statements to get it to work. E.g. this
x = A(1){1}{1} <-- not supported by MATLAB
would need to be something like this instead
temp = A(1)
x = temp{1}{1}
  4 commentaires
Minh Tran
Minh Tran le 18 Sep 2018
Modifié(e) : James Tursa le 18 Sep 2018
A1 = [1 2 3; 4 5 6;]; % Container is a `regular matrix` so the return type of A1(1) is a regular matrix (of size 1x1 in this case)
A2 = {1 2 3; 4 5 6}; % Container is a `cell array` so the return type of A2(1) is a cell array of size 1x1. A2(1,:) would refer to a 1x3 cell array whose elements are 1x1 `regular matrices`.
A3 = {{1} {2} {3}; {4} {5} {6}}; % Like A2, A3(1) refers to a cell array but each element is another cell array.
Does that sound right?
James Tursa
James Tursa le 18 Sep 2018
Yes, you have it right.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 18 Sep 2018
a is a cell array where each cell contains a single cell.
b is a cell array where each cell contains a string (character vector).
Have you read the FAQ on cell arrays? https://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
I think you should have a pretty good intuitive feel for cells and cell arrays after reading that.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2007a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by