Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.

Similar presentations


Presentation on theme: "MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University."— Presentation transcript:

1 MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University

2 MATLAB Cell Arrays © 2011 Greg Reese. All rights reserved

3 'Romeo, Romeo ' 3.1415 99 @my_function() 3 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 100 010 001

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

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

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

7 7 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 a{3}{1}{1,2} = 2 a{3}{1}{2,2} = 4 cell within cell

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

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

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

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

12 12 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 Can only use on one cell at a time (sort of - will discuss this later) Use content indexing if you want to get inside cell

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

14 14 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 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 "Attributes" column always empty for these slides so will omit from now on

15 15 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 >> z = a{1} z = AAA >> whos z Name Size Bytes Class z 1x3 6 char Why?

16 16 Cell Arrays - indexing Examples - follow along >> z = a(2) z = [2] >> whos z Name Size Bytes Class z 1x1 68 cell >> z = a{2} z = 2 >> whos z Name Size Bytes Class z 1x1 8 double Why?

17 17 Cell Arrays - indexing Examples - follow along >> z = a(3) z = {1x1 cell} >> whos z Name Size Bytes Class z 1x1 392 cell >> z = a{3} z = {2x2 cell} >> whos z Name Size Bytes Class z 1x1 332 cell Why? This means a cell containing a 1x1 cell

18 18 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. >> b(1) = 'Hello' ??? Conversion to cell from char is not possible. >> b(1) = a ??? In an assignment A(:) = B, the number of elements in A and B must be the same. Why?

19 19 Cell Arrays - indexing Examples - follow along a = 'AAA' [2] {1x1 cell} >> b(1) = a(1) b = 'AAA' [1x4 double] >> b(1) = a{1} ??? Conversion to cell from char is not possible. >> b(1) = a(2) b = [2] [1x4 double] >> b(1)= a{2} ??? Conversion to cell from double is not possible. >> b(1) = a(3) b = {1x1 cell} [1x4 double] >> b(1) = a{3} b = {2x2 cell} [1x4 double] Why?

20 20 Cell Arrays - indexing Examples - follow along Store into single, content-indexed cell >> a a = 'AAA' [2] {1x1 cell} >> b = bb b = 'B' [1x4 double] >> b{1} = a(1) b = {1x1 cell} [1x4 double] >> b{1} = a{1} b = 'AAA' [1x4 double]

21 21 Cell Arrays - indexing Examples - follow along a = 'AAA' [2] {1x1 cell} b = 'B' [1x4 double] >> b{1} = a(2) b = {1x1 cell} [1x4 double] >> b{1} = a{2} b = [2] [1x4 double] >> b{1} = a(3) b = {1x1 cell} [1x4 double] >> b{1} = a{3} b = {1x1 cell} [1x4 double]

22 22 Cell Arrays - indexing Tricky case - content indexing of multiple cells MATLAB says you can't do it Really means that you can do it but under many circumstances, content of all but first cell disappear For more information, see "comma- separated list" in MATLAB help

23 23 Cell Arrays - indexing Accessing a range of cells with content indexing (curly braces) produces a comma- separated list. If there are n elements in the range, there will be n members of the list and these will be assigned in order to the output variables. If there aren't enough output variables, MATLAB ignores remaining list members data{4:7} → data{4} data{5} data{6} data{7} [ a b c ] = data{4:7} same as [ a b c ] = data{4} data{5} data{6} data{7}

24 24 Cell Arrays - indexing a = 'AAA' [2] {1x1 cell} >> clear x >> x = a{1:3} x = AAA >> whos x Name Size Bytes Class x 1x3 6 char >> x = a{2:3} x = 2 >> whos x Name Size Bytes Class x 1x1 8 double

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

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

27 27 Cell Arrays - indexing Tip Avoid specifying more than one cell when content indexing a{1:4,:}

28 28 Cell Arrays - indexing Content of a cell-array element is often a (numerical) array. You can access multiple array elements of a single element in a cell array. a{m,n}(r1:r2,c1:c2) returns an array made up of rows r1 through r2 and columns c1 through c2 of the element at row m and column n of the cell array a

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

30 30 Cell Arrays - indexing Examples >> c{2,1}(:,:) ans = 1 2 3 4 >> c{2,1}(:) ans = 1 3 2 4

31 31 Cell Arrays - numbers MATLAB functions that use numbers in computations seldom work on numbers that are in cells. 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);

32 32 Cell Arrays - numbers However, for convenience, numbers may appear in cells. To convert a cell array of numbers to a numerical array, use: n = cell2mat( c ) c is a cell array with only numbers n is a numerical (double) array –If elements of c are all scalars *, n has same dimensions as c * The elements can also be matrices. See MATLAB help on cell2mat() for details

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

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

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

36 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] 36 Cell Arrays - numbers

37 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 37 Cell Arrays - numbers

38 Try It Store all data in one cell array with the first column containing the names and the second the ages 38 Cell Arrays - numbers

39 Try It >> ages = [ 40 45 50 ] ages = 40 45 50 >> cAges = num2cell( ages )' cAges = [40] [45] [50] >> guys = cell( 3, 2 ) guys = [] [] [] [] 39 Cell Arrays - numbers

40 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 40 Cell Arrays - numbers

41 41 Cell Arrays - functions on Some MATLAB functions will take a cell array as input, e.g., cell2mat(), isvector(), and deal(). Others, such as mean(), eval(), and not(), won't. If one of the latter functions can accept the content of the elements of the cell array as input, we can process those cells with the MATLAB function cellfun()

42 42 Cell Arrays - functions on Even if a function can take a cell array as an input, we may want to apply a function that can accept cell-array arguments to the content of a cell array, not to the array itself Example Find the length of each word in a string array >> c = { 'a' 'bb' 'cccc' } c = 'a' 'bb' 'cccc' >> length(c) ans = 3 length of cell array c, not lengths of content (1, 2, 4)

43 43 Cell Arrays - functions on The MATLAB function cellfun() applies a specified function to the content of every element of a cell array

44 44 Cell Arrays - functions on We'll use a basic version of cellfun, namely, A = cellfun( fun, C ) C is a cell array fun is a function that takes exactly one input value and produces exactly one, scalar output –fun must be able to accept the type of input in the cells of C, e.g., character array, numerical array, etc. A is an array of same dimension as C

45 45 Cell Arrays - functions on

46 46 Cell Arrays - functions on A = cellfun( fun, C ) To specify the function, use the at-sign @ followed by the function name without parentheses. (This is called a function handle) Examples –If C is a cell array of strings, cellfun( @length, c ) finds the length of each of the strings in the array –If C is a cell array of strings, cellfun( @isempty, c ) tells whether or not each cell in the array is empty

47 47 Cell Arrays - functions on Example Find the means of four data sets: >> data = { 1 [0 2 4] [ -1 0 1] randn(1,1000) } data = [1] [1x3 double] [1x3 double] [1x1000 double] >> mean( data ) ??? Undefined function or method 'sum' for input arguments of type 'cell'. Error in ==> mean at 28 y = sum(x)/size(x,dim); >> dataMeans = cellfun( @mean, data ) dataMeans = 1.0000 2.0000 0 -0.0326 >> whos data dataMeans Name Size Bytes Class data 1x4 8296 cell dataMeans 1x4 32 double mean() doesn't accept cell arrays as input, but contents of each array is okay because those are numerical arrays

48 48 Cell Arrays - functions on tf = isempty( C ) isempty() is a useful function which returns 1 (true) if its input array is empty and 0 (false) if the array is not empty. (An empty array is one that has at least one dimension of size zero, e.g., 0x0, 0x5, 3x7x0 )

49 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 49 Cell Arrays - functions on

50 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 50 Cell Arrays - functions on

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

52 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 52 Cell Arrays - functions on

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

54 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 54 Cell Arrays - functions on

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

56 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 ) ) indexes = 3 56 Cell Arrays - finding

57 57 Cell Arrays - finding Can also use find() to determine if any of the cell array members meet a criterion and if so, how many meet it: Length of returned index vector is number of cells meeting criterion –If length is zero or returned vector is empty (same thing), no cells meet criterion

58 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 58 Cell Arrays - finding

59 59 Cell Arrays Questions?

60 60 The End


Download ppt "MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University."

Similar presentations


Ads by Google