You are on page 1of 39

MATLAB

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

Use cell indexing if you want to


manipulate cells of an array, regardless
of what is inside them
9
Cell Arrays - indexing
To delete elements of a cell array, use cell
indexing ()
Examples
>> a = { 'r1c1' 'r1c2' 3; [1 2; 3 4] 1:4 'The End' }
a = 'r1c1' 'r1c2' [ 3]
[2x2 double] [1x4 double] 'The End'
>> a(:,2) = [] % delete second column
a = 'r1c1' [ 3]
[2x2 double] 'The End'
>> a(2,:) = [] % delete second row
a = 'r1c1' [3]

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

Even though a is a cell array, output from first line is


showing content of array's cells
– 'AAA' means that the content of cell 1 of a is a character
array with three elements
– [2] means that the content of cell 2 of a is a numerical
array with one element (the number 2)
– {1x1 cell} means that the content of cell 3 of a is a
cell array with one cell
13
Cell Arrays - indexing
Examples - follow along
From before
a = 'AAA' [2] {1x1 cell}

>> 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

Examples - follow along


>> z = a(2)
z = [2]
>> whos z
Name Size Bytes Class
z 1x1 68 cell Why?

>> z = a{2}
z = 2
>> whos z
Name Size Bytes Class
z 1x1 8 double Why?

15
Cell Arrays - indexing

Examples - follow along


>> z = a(3)
z = {1x1 cell} This means a cell containing a 1x1 cell
>> whos z
Name Size Bytes Class
z 1x1 392 cell Why?

>> 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

z 1x1 332 cell

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]

Can do this a little more easily by concatenating the


two cell arrays. See MATLAB help 29
Cell Arrays - functions on
Try It
Make a cell array of four cells whose
contents are 'a', 'bb', empty ( {} ), and
'ccc'. Determine if the array is empty
and if each cell is empty
>> c = { 'a' 'bb' {} 'cccc' }
c = 'a' 'bb' {} 'cccc'
>> isempty( c )
ans = 0
>> cellfun( @isempty, c )
ans = 0 0 1 0 30
Cell Arrays - functions on
Try It
Use the previous array and find the
length of the array and the number of
characters in each string in the array

>> length( c )
ans = 4

>> cellfun( @length, c )


ans = 1 2 0 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

>> any( cellfun( @length, c ) )


ans = 1
33
Cell Arrays - functions on
It's also common to want to know if all
elements of a cell array meet a certain
condition. Do this with the MATLAB function:
B = all( A )
• A is a numerical vector (not a cell array)
• B is a scalar that is 1 if all elements of A are
not zero and 0 otherwise, i.e., if at least one
element of A is zero
all() also works on matrices. See MATLAB
help for details
34
Cell Arrays - functions on
Try It
Use the previous array and determine
if all of the strings have at least one
character
>> lengths = cellfun( @length, c )
lengths = 1 2 0 4
>> all( lengths )
ans = 0

>> all( cellfun( @length, c ) )


ans = 0
35
Cell Arrays - finding
Often it's useful to know which cells in a cell
array meet a certain condition. One way to find
out is with the MATLAB function:
indexes = find( A )
• A is a numerical vector (not a cell array)
• indexes is a vector of the indexes of the
cells whose content is not 0
– If no such cells, indexes is empty
• find() also works on matrices. See
MATLAB help for details
36
Cell Arrays - finding
Try It
Use the previous array and find the
indexes of all empty cells
>> c = { 'a' 'bb' {} 'cccc' }
c = 'a' 'bb' {} 'cccc'
>> empty = cellfun( @isempty, c )
empty = 0 0 1 0
>> indexes = find( empty )
indexes = 3
>> c{3}
ans = {}

Often just do it all at once


>> indexes = find( cellfun( @isempty, c ) )
37
indexes = 3
Cell Arrays - finding
Try It
Use the previous array and find()
to determine how many empty cells
are in the array
>> length( find( cellfun( @isempty, c ) ) )
ans = 1

38
The
End
39

You might also like