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

Slides:



Advertisements
Similar presentations
Multidimensional Array
Advertisements

Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Introduction to C Programming
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Lists Introduction to Computing Science and Programming I.
Chapter 10.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Chapter 9: Arrays and Strings
Cells and Structures Array Cells and Array Structures.
Chapter 8 Arrays and Strings
Introduction to C Programming
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Java Unit 9: Arrays Declaring and Processing Arrays.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
AN ENGINEER’S GUIDE TO MATLAB
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
 2006 Pearson Education, Inc. All rights reserved Arrays.
GUI Input and Output Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
Fall 2006AE6382 Design Computing1 Cell Arrays and Structures Learning Objectives Learn about characters, cell arrays & structures Topics Data Types Character.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
MATLAB Logical Indexing Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Cell Arrays Lecture 2/8/ Data Types (Review) 2. General Concept 3. Using Cell-Arrays 1. Syntax/Symbols 2. Dialog Boxes 1.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
1. Normal arrays of characters 2. Converting a list of strings to a cell- arrays of strings 3. Converting a cell-array of strings to a list of strings.
Cell Arrays: An Introduction Without an understanding of how cell arrays work and how to interact with them, cell arrays can be one of the most frustrating.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
An Introduction to Programming in Matlab Emily Blumenthal
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Computer Programming BCT 1113
Chapter 2 Creating Arrays.
© 2016 Pearson Education, Ltd. All rights reserved.
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Chapter 8 Data Structures: Cell Arrays and Structures
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Chapter # 2 Part 2 Programs And data
Chapter 8 Data Structures: Cell Arrays and Structures, Sorting
Presentation transcript:

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

MATLAB Cell Arrays © 2011 Greg Reese. All rights reserved

'Romeo, Romeo ' 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

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 = [ i] [1x4 double] 'carrots' [2x2 double] [ 77] [ NaN]

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Cell Arrays - indexing Tip Avoid specifying more than one cell when content indexing a{1:4,:}

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 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 Cell Arrays - indexing Examples >> c{2,1}(:,:) ans = >> c{2,1}(:) ans =

31 Cell Arrays - numbers MATLAB functions that use numbers in computations seldom work on numbers that are in cells. Example >> c = { } 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 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 Cell Arrays - numbers Example >> c = { } 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 = >> mean( cNum ) ans = 4

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 Cell Arrays - numbers Example >> a = magic( 3 ) a = >> c = num2cell( a ) c = [8] [1] [6] [3] [5] [7] [4] [9] [2]

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 = { } ages = [40] [45] [50] 36 Cell Arrays - numbers

Try It Find the mean age >> nAges = cell2mat( ages ) nAges = >> mean( nAges ) ans = 45 or all at once: >> mean( cell2mat( ages ) ) ans = Cell Arrays - numbers

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

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

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 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 Cell Arrays - functions on The MATLAB function cellfun() applies a specified function to the content of every element of a cell array

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

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

47 Cell Arrays - functions on Example Find the means of four data sets: >> data = { 1 [0 2 4] [ ] 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 = data ) dataMeans = >> whos data dataMeans Name Size Bytes Class data 1x 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 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 )

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 >> c ) ans = 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 >> c ) ans = Cell Arrays - functions on

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

Try It Use the previous array and determine if any of the strings have at least one character >> lengths = c ) lengths = >> any( lengths ) ans = 1 >> any( c ) ) ans = 1 52 Cell Arrays - functions on

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

Try It Use the previous array and determine if all of the strings have at least one character >> lengths = c ) lengths = >> all( lengths ) ans = 0 >> all( c ) ) ans = 0 54 Cell Arrays - functions on

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

Try It Use the previous array and find the indexes of all empty cells >> c = { 'a' 'bb' {} 'cccc' } c = 'a' 'bb' {} 'cccc' >> empty = c ) empty = >> indexes = find( empty ) indexes = 3 >> c{3} ans = {} Often just do it all at once >> indexes = find( c ) ) indexes = 3 56 Cell Arrays - finding

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

Try It Use the previous array and find() to determine how many empty cells are in the array >> length( find( c ) ) ) ans = 1 58 Cell Arrays - finding

59 Cell Arrays Questions?

60 The End