What is the difference between ( ) and { } when accessing elements of a cell array?
154 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 { }?
0 comentarios
Respuestas (2)
James Tursa
el 18 de Sept. de 2018
Editada: James Tursa
el 18 de Sept. de 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 comentarios
Image Analyst
el 18 de Sept. de 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).
I think you should have a pretty good intuitive feel for cells and cell arrays after reading that.
Ver también
Categorías
Más información sobre Data Type Identification en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!