Professional Documents
Culture Documents
Cell Arrays
Cell Arrays - definition
A MATLAB cell array is an array of cells.
A cell can hold any data type, and data
types within a cell array can differ
1 0 0
0 1 0
'Romeo, Romeo' 3.1415
0 0 1
@my_function()
99
2
Cell Arrays - creation
For specified content, create same way as
arrays but use (curly) braces
Example
>> a = { i 5:-1:2 'carrots'; magic(2) 77 NaN }
a =
[0 + 1.0000i] [1x4 double] 'carrots'
[2x2 double] [ 77] [ NaN]
3
Cell Arrays - creation
Create empty cell array with cell() function
a = cell( rows, columns )
Example
>> a = cell( 3, 6 )
a =
[] [] [] [] [] []
[] [] [] [] [] []
[] [] [] [] [] []
>> whos a
Name Size Bytes Class
a 3x6 72 cell
4
Cell Arrays - display
Various ways to display cell array. One is
to type variable name with no trailing
semicolon. This shows top level of array
only
Example
>> a = { 'AAA' 2 { {1 2; 3 4} } };
>> a
a =
'AAA' [2] {1x1 cell}
5
Cell Arrays - display
celldisp() recursively displays cell
array, i.e., if content a cell array, also
displays its content
Example
>> a = { 'AAA' 2 { {1 2; 3 4} } };
>> celldisp( a )
a{1} = AAA
a{2} = 2
a{3}{1}{1,1} = 1
a{3}{1}{2,1} = 3
cell within cell
a{3}{1}{1,2} = 2
a{3}{1}{2,2} = 4 6
Cell Arrays - display
cellplot() displays in figure window
drawing of 1D or 2D cell array
Example
>> a = { 'AAA' 2 { {1 2; 3 4} } };
>> cellplot( a )
1 2
AAA 2
3 4
7
Cell Arrays - indexing
Two types of indexing into a cell array
• Cell indexing - access groups of cells
but not their content
• Content indexing - access single cell's
content
8
Cell Arrays - indexing
Cell indexing
• Done by using parentheses around
subscript, e.g., a(5), b(3:6,:), c(2,9)
• Can access one or more cells
• Cannot access content of cells
10
Cell Arrays - indexing
Content indexing
• Done by using (curly) braces around
subject, e.g., a{5}, b{2,9}
• Lets you access what's inside a
single cell
To create cell array use
A=cell(2,1,2)
11
Cell Arrays - indexing
Principal rules
• Indexing a cell(s) with () always produces cells
• Indexing a cell with {} produces a data type of
whatever is inside the indexed cell
– Result of {} indexing is a cell only if content is a cell
• If have cells indexed with () on left side of =,
must have array of cells of same size on right
side
• If array on left not subscripted, can use () or {}
on right
12
Cell Arrays - indexing
Examples - follow along
>> a = { 'AAA' 2 { {1 2; 3 4} } }
a = 'AAA' [2] {1x1 cell}
>> whos a
Name Size Bytes Class Attributes
a 1x3 526 cell
"Attributes" column always
empty for these slides so
will omit from now on
>> z = a(1)
z = 'AAA'
>> whos z
Name Size Bytes Class
z 1x1 66 cell Why?
>> z = a{1}
z = AAA
>> whos z
Name Size Bytes Class
z 1x3 6 char
Why?
14
Cell Arrays - indexing
>> z = a{2}
z = 2
>> whos z
Name Size Bytes Class
z 1x1 8 double Why?
15
Cell Arrays - indexing
>> z = a{3}
z = {2x2 cell}
>> whos z
Name Size Bytes Class
z 1x1 332 cell Why?
16
Cell Arrays - indexing
Examples - follow along
>> bb = { 'B' 6:9 }
bb = 'B' [1x4 double]
>> b = bb;
>> whos b
Name Size Bytes Class
b 1x2 154 cell
>> b(1) = 6
??? Conversion to cell from double is not possible. Why?
>> b(1) = 'Hello'
??? Conversion to cell from char is not possible.
Why?
>> b(1) = a
??? In an assignment A(:) = B, the number of elements
Why?
in A and B must be the same.
17
Cell Arrays - indexing
>> clear x y
>> [ x y ] = a{1:3}
x = AAA
y = 2
>> whos x y
Name Size Bytes Class
x 1x3 6 char
y 1x1 8 double
18
Cell Arrays - indexing
>> clear x y z
>> [ x y z ] = a{1:3}
x = AAA
y = 2
z = {2x2 cell}
>> whos x y z
Name Size Bytes Class
x 1x3 6 char
y 1x1 8 double
19
Cell Arrays - indexing
Tip
Avoid specifying more than one cell
when content indexing
a{1:4,:}
20
Cell Arrays - indexing
Examples
>> c = { 'CC' 7; [1 2; 3 4] [0+i 1+0i] }
c = 'CC' [ 7]
[2x2 double] [1x2 double]
>> c{2,1}(2,2)
ans = 4
>> c{2,1}(:,2)
ans = 2
4
21
Cell Arrays - indexing
Examples
>> c{2,1}(:,:)
ans = 1 2
3 4
>> c{2,1}(:)
ans = 1
3
2
4
22
Cell Arrays - numbers
Example
>> c = { 1 3 5 7 }
c = [1] [3] [5] [7]
>> mean(c)
??? Undefined function or method 'sum' for input
arguments of type 'cell'.
Error in ==> mean at 28
y = sum(x)/size(x,dim);
>> cNum = cell2mat( c )
cNum = 1 3 5 7
>> mean( cNum )
ans = 4
23
Cell Arrays - numbers
To convert an array of numbers to a cell
array, use:
c = num2cell( A )
• A is a numerical array
• c is a cell array of same dimension as A
24
Cell Arrays - numbers
Example
>> a = magic( 3 )
a = 8 1 6
3 5 7
4 9 2
>> c = num2cell( a )
c = [8] [1] [6]
[3] [5] [7]
[4] [9] [2]
25
Cell Arrays - numbers
Try It
Suppose you are given the names and
ages of three guys in two arrays, like
this:
>> names = { 'John' 'Joe' 'Jeff' }
names = 'John' 'Joe' 'Jeff'
>> ages = { 40 45 50 }
ages = [40] [45] [50]
26
Cell Arrays - numbers
Try It
Find the mean age
>> nAges = cell2mat( ages )
nAges = 40 45 50
>> mean( nAges )
ans = 45
or all at once:
>> mean( cell2mat( ages ) )
ans = 45
27
Cell Arrays - numbers
Try It
>> ages = [ 40 45 50 ]
ages = 40 45 50
>> cAges = num2cell( ages )'
cAges = [40]
[45]
[50]
>> guys = cell( 3, 2 )
guys = [] []
[] []
[] []
28
Cell Arrays - numbers
Try It
>> guys(:,1) = names'
guys = 'John' []
'Joe' []
'Jeff' []
>> guys(:,2) = cAges
guys = 'John' [40]
'Joe' [45]
'Jeff' [50]
>> length( c )
ans = 4
31
Cell Arrays - functions on
It's common to want to know if any element of
a cell array meets a certain condition. Do this
with the MATLAB function:
B = any( A )
• A is a numerical vector (not a cell array)
• B is a scalar that is 1 if any element of A is
not zero and 0 otherwise, i.e., if all elements
of A are zero
any() also works on matrices. See MATLAB
help for details
32
Cell Arrays - functions on
Try It
Use the previous array and determine
if any of the strings have at least one
character
>> lengths = cellfun( @length, c )
lengths = 1 2 0 4
>> any( lengths )
ans = 1
38
The
End
39