COMP 116: Introduction to Scientific Programming Lecture 25: Cell Arrays and Structures.

Slides:



Advertisements
Similar presentations
Matlab Tutorial Course
Advertisements

COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Cells and Structures.
CMPS 1371 Introduction to Computing for Engineers STRUCTURE ARRAYS.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
Introduction to MATLAB
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Matlab tutorial course Lesson 2: Arrays and data types
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Fall 2006AE6382 Design Computing1 OOP: Creating a Class More OOP concepts An example that creates a ASSET class and shows how it might be used Extend the.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
COMP 116: Introduction to Scientific Programming Lecture 6: Scripts and publishing, Creating matrices.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
COMP 116: Introduction to Scientific Programming Lecture 5: Plotting, Scripts and publishing.
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
COMP 116: Introduction to Scientific Programming Lecture 24: Strings in MATLAB.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
MATLAB MATrix LABoratory. MATLAB Desktop Command window – input and output Command window – input and output Command history – records commands given.
CS112 Scientific Computation Department of Computer Science Wellesley College Numb3rs Number and image types.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Working with Arrays in MATLAB
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.
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
Covenant College December 18, Laura Broussard, Ph.D. Visiting Professor COS 131: Computing for Engineers Chapter 7: Cell Arrays and Structures.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Introduction to Literate Programming in Matlab 2WN50 – Week programming-in-matlab.pptx?dl=0.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
CS100A, Fall 1998, Lecture 191 CS100A, Fall 1998 Lecture 19, Thursday Nov 05 Matlab Concepts: Matlab arrays Matlab subscripting Matlab plotting.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
An Introduction to Programming in Matlab Emily Blumenthal
Matlab & Image processing toolbox Heejune Ahn SeoulTech Last updated Jan.
Introduction to MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Chapter 8 Data Structures: Cell Arrays and Structures
Announcements P3 due today
Chapter 8 Data Structures: Cell Arrays and Structures, Sorting
Introduction to Computer Science
Presentation transcript:

COMP 116: Introduction to Scientific Programming Lecture 25: Cell Arrays and Structures

Announcements and Reminders Friday class will be taught by Sachin Patil This Wednesday’s quiz moved to Friday This Wednesday office hours from 2-3pm Midterm coming up

Today How to store variables of different types (numbers, characters, strings, arrays, matrices) all in one single array? Two options: ◦ Cells ◦ Structs

Data Structures Arrays ◦ Homogenous ◦ Indexing by numbers Cell Arrays ◦ Heterogeneous ◦ Indexing by numbers Structures ◦ Heterogeneous ◦ Accessing by naming

REVIEW: ARRAYS

Multi-dimensional Arrays [42] ◦ Scalar ◦ 0-dimensional [ ] ◦ Vector ◦ 1-dimensional [1 2 3; 4 5 6] ◦ Matrix ◦ 2-dimensional We can go higher: ◦ Multidimensional array ◦ n-dimensional

N-D arrays 3 -Dimensional arrays ◦ e.g. temperature at every point in this room (x,y,z) 4 -Dimensional arrays ◦ e.g. temperature in this room over time (x,y,z,t) >> A = ones(2,3,2); >> B(:,:,1) = [1:4; 5:8]; >> B(:,:,2) = [7:10; 13:16];

Most common 3D array Images ◦ 3 channels (red, green, blue) ◦ Each channel is a 2D array

Most common 3D array

N-D Arrays: Arithmetic Element-by-Element arithmetic ◦ The same as before ◦ + -.*./.\.^ Linear algebra (i.e., matrix) operations generally not used

CELL ARRAYS >> doc cell Under Help: MATLAB → Programming Fundamentals → Classes (Data Types) → Cell Arrays

Creating Cell Arrays Just like arrays, but use {} instead of [] Here’s a list of names: ◦ Alexander the Great ◦ Winnie the Pooh ◦ Jack the Ripper ◦ Stephen the Colbert Put these into a MATLAB data structure

Creating Cell Arrays Just like arrays, but use {} instead of [] Does this work? ◦names = ['Alexander the Great'; 'Winnie the Pooh'; 'Jack the Ripper'; 'Stephen the Colbert'] Simple modification: ◦names = {'Alexander the Great'; 'Winnie the Pooh'; 'Jack the Ripper'; 'Stephen the Colbert'}

Cell Arrays ArraysCell Arrays Elements accessed by indexing using numbers, A(r,c) or A(idx). Elements accessed by indexing using numbers, A(r,c) or A(idx) Each element in array must all have same type Each element can have any type Elements of an array are ‘simple’ Each element can contain another array or cell array Strings stored in Arrays must all be same length Strings stored in cell arrays can all be different lengths Note: Not the same concept as cells (%) in publishable scripts

Creating Cell Arrays % Cell array of different length strings days = {'Monday'; 'Tuesday';...}; % Cell array of 4 elements (of different sizes, data types) some_guy = { 'Steve', 45;... [ ], {'ice cream'; 'pizza'} }; % Using 'cell' command cB = cell(n) % creates n by n cell of empty elements cC = cell( m, n) % creates m by n cell of empty elements % Creating a Cell Array from an Array A = ones(2, 2); cA = cell(size(A)); % Note: all elements are empty

Accessing Cell Arrays: % Change content some_guy{1,1} = 'Chuck' some_guy{2,2} = 1:5; some_guy{1,2} = []; % Delete content in cell 1,2 % Grab 1 st, 2 nd elements some_guy(1:2) row2 = some_guy (2,:) % Grab 2 nd row of cells Two ways to access elements: names{2} gives you the contents of a call names(2) gives you a new cell array with those contents

Another useful example names = {'Sine', 'Cosine'}; labels = {'0', 'pi', '2pi', '3pi', '4pi'}; t = 0:pi/32:4*pi; plot(t, [sin(t); cos(t)]); legend(names); set(gca, 'XTick', 0:pi:4*pi); set(gca, 'XTickLabel', labels);

Accessing Cell Arrays Remember: crucial distinction between {} and () operators Example: A = { false, rand(3); 4.0, 'This is a string'};  A{1}  A{1,1}  A(2)  A(2,1)  A(2,:)  A{:,2} Indices work just as for ‘standard’ arrays.

Useful Commands celldisp – shows contents of the cell in the command winow, one element at a time cellplot – graphical display of all cells in a cell array

Inspecting Cell Arrays A = { false, rand(3); 4.0, 'This is a string'}; Try: disp(A) celldisp(A) cellplot(A) Now try this: >> B = {A; A} >> D = { B B } >> cellplot(D);

Practice A = {4, [1:10]', 'demo'; false, eye(2), rand(3)}; A B = A(1) C = A{2} D = A(2,[2 3]) E = {A{1, [1 3]}, 20} F = {A(1, [1 3]), 20}

Examples Collection of strings ◦ Extract important words from a document

Exercise Write a function get_the_words that ◦ Takes as an input a sentence (in the form of a string) e.g. ‘This is a true statement’ ◦ And returns a cell array of the words in the sentence i.e. {‘This’, ‘is’, ‘a’, ‘true’, ‘statement’}

STRUCTURES Under Help … MATLAB → Programming Fundamentals → Classes (Data Types) → Structures

Structures StructuresCell Arrays Elements known by name Elements known by number (indexing). Elements called fields Each field can be any type But the objects must be consistent Each element (cell) can have any type Each element inside a structure is given a unique name These elements are called fields Structures are a common concept in most programming languages

Structures Same example as before, but now with more data NameOccupationBirthFictional Alexander the GreatConqueror356 BCNo Winnie the PoohBear1926Yes Jack the RipperSerial Killer1800sNo Stephen the ColbertPolitical Pundit1964Hmm

Structures Use named ‘fields’ for each variable alex.name = 'Alexander the Great'; alex.occupation = 'Conqueror'; alex.birth = 356; alex.fictional = false;

Structures Use named ‘fields’ for each variable Use the struct() function, with name-value pairs alex.name = 'Alexander the Great'; alex.occupation = 'Conqueror'; alex.birth = 356; alex.fictional = false; alex = struct('name', 'Alexander the Great',... 'occupation', 'Conqueror',... 'birth', 356,... 'fictional' = false);

Structure Arrays Each individual structure object is accessed with its index (just as in vectors) Each field inside an individual structure is found by naming using the '.' operator for access. characters(1) = alex; characters(2).name = 'Winnie the Pooh'; characters(2).occupation = 'Bear';... characters(3) = struct('name', 'Jack the Ripper',...);

Structure Arrays One way to initialize is to use a ‘template’ % create structure layout % note the use of default values and empty arrays template = struct( 'name', 'no name',... 'nickname', 'no name',... ' s', [],... 'department', 'undeclared',... 'type', 'undergrad',... 'year', 1 ); % create structure array students = repmat( template, 1, 30 ); % now fill in each structure in the array

Examples Nested records ◦ Structures can contain other structures (or cell arrays) as named fields. class.instructor = struct('name', {' Vishal' ‘Verma'}); class.students = students; % this is a struct array class.building = 'FB'; class.room = 7;

Examples Built-in structures ◦ Figures get(gcf) ◦ File lists >> files = dir files = 8x1 struct array with fields: name date bytes isdir datenum

Examples The dir function returns a structure array with 4 fields: name, date, byte, isdir ◦ The input of dir is a path How do we use this to find out the total size of all the files in a directory? How do we get all of the names of all images in a folder?

Common Pitfalls Remember the difference between {} and () when accessing cell ◦ () returns cells ◦ {} returns contents Correctly access the field of a structure via the dot operator and the fieldname. ◦currStudent.name = 'Steve'; Remember: you still need the index () operator for working with Structure arrays ◦students(7).score = 97;

Style Guidelines Use arrays when all elements are the same type and in some sense represent the same thing (just different values) Use cell arrays or structures when the values that make up the object are logically grouped but not the same type or the same thing. ◦ Prefer cell arrays when you want to loop through all elements in each individual cell via indexing. ◦ Prefer structures when you want to name each element individually Use cell arrays for storing an array of strings.

Summary Review Cell Arrays & Structures  doc cell  MATLAB → Programming Fundamentals → Classes (Data Types) → Cell Arrays  MATLAB → Programming Fundamentals → Classes (Data Types) → Structures Practice working with  Cell Arrays  Structures  Operators for Cell Arrays & Structures  {}, (), '.'

Useful functions rmfield – removes a field from a structure isstruct – tests whether a variable is of type structure isfield – tests whether a name string is a field in the specified structure. fieldnames – returns names of all fields in a struture.