Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed...

11
Cell Arrays UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~ pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative

Transcript of Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed...

Page 1: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Cell ArraysUC Berkeley

Fall 2004, E77http://jagger.me.berkeley.edu/~pack/e77

Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to

Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Page 2: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Cell Arrays (chapter 7, page 101-115)

Numeric arrays (class name is double) are rectangular arrays of numbers.

Character arrays (class name is char) are rectangular arrays of characters.

Cell arrays (class name is cell) are rectangular arrays of containers, whose contents may be

–double arrays–character arrays, or even–cell arrays

Page 3: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Review of Cell Arrays: Creating

Use curly brackets { and } to wrap a variable in a container.

Example: Create a 1-by-1 container, whose contents is the 1-by-12 character array ‘Andy Packard’

>> name = { ’Andy Packard’ };

>> size(name)

>> class(name)

Create two more 1-by-1 containers with different contents

>> SID = { 12345678 };

>> scores = { [82 71 64 88 99] };

Page 4: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Concatenating

Stack all three containers next to one another, creating a 1-by-3 array of containers.

>> ClassInfo = [ name SID scores ];

ClassInfo is a 1-by-3 array of containers. It is called a cell array.

>> size(ClassInfo)>> class(ClassInfo)>> whos

998864718212345678'' PackardAndy

Container 1

Contents of Container 1

Page 5: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

class and size for cell arrays

ClassInfo is a 1-by-3 array of containers--a cell array.

>> size(ClassInfo)

>> class(ClassInfo)

>> whosUsing parenthesis () to access parts of the array keeps the containers (remember, it is an array of containers).

>> ClassInfo(1)

>> tmp = ’Andy Packard’

>> isequal(ClassInfo(1),’Andy Packard’)

>> size(ClassInfo(1))

>> class(ClassInfo(1))

Page 6: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Referencing contents of cells

Use curly brackets to access the contents of a container.

>> ClassInfo{1}

>> ClassInfo{2}

>> ClassInfo{3}

>> isequal(ClassInfo{2},12345678)

>> isequal(ClassInfo{1},tmp)

Page 7: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

3-by-2 Cell Array example

Create a 3-by-2 array of containers

>> A = [ {[1 2;3 4]} {’andy’} ;...

{pi} {rand(20,2)}; ...

{[’E77’;’fun’]} {zeros(1,6)} ];

Page 8: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

000000''

'77'

......

462.0763.0

934.0126.0

...14.3

''43

21

fun

E

andy

>> A = [ {[1 2;3 4]} {’andy’} ;...

{pi} {rand(20,2)}; ...

{[’E77’;’fun’]} {zeros(1,6)} ];

View it as 3-by-2 array of containers

2-by-2 double array in container 1,1

1-by-4 char array in container 1,2

Page 9: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Quiz: References into a 3-by-2 Cell Array

>> size(A)

>> class(A)

>> B = A(1,1);

>> size(B), class(B)

>> C = A{1,1}

>> size(C), class(C)

>> A{1,1}(2,2)

>> D = A(:,2)

>> size(D), class(D)

>> D{3}(3)

>> [’s’ D{1} ’ beach’]

000000''

'77'

......

462.0763.0

934.0126.0

...14.3

''43

21

fun

E

andy

Recall what A is

Page 10: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

CELL creates Cell Arrays with empty contents

Create a 3-by-4 array of containers. By default, the contents of each container will be an empty array (0-by-0) double array.

>> E = cell(3,4)Put the array [7 8 9] in the contents of the 1,1 container

>> E{1,1} = [ 7 8 9 ];Try to insert the array [ 20 25 ] as the 1,2 container

>> E(1,2) = [ 20 25 ];Insert a container (whose contents is the array [ 20 25 ]) in the 1,2 element of E (recall, all elements of E are containers)

>> E(1,2) = { [ 20 25 ] };What is different in

>> E{1,3} = { [ 20 25 ] };

Page 11: Cell Arrays UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Commands to look at on your own

num2cell

cell2mat

mat2cell

cellstr

celldisp

iscell