Basic Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Interfaces Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
More Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
CSC 4630 Meeting 9 February 14, 2007 Valentine’s Day; Snow Day.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Program Input and the Software Design Process ROBERT REAVES.
Introduction Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
5  Systems of Linear Equations: ✦ An Introduction ✦ Unique Solutions ✦ Underdetermined and Overdetermined Systems  Matrices  Multiplication of Matrices.
Chapter 10 Review: Matrix Algebra
1 資訊科學數學 14 : Determinants & Inverses 陳光琦助理教授 (Kuang-Chi Chen)
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Row rows A matrix is a rectangular array of numbers. We subscript entries to tell their location in the array Matrices are identified by their size.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 5 Systems and Matrices Copyright © 2013, 2009, 2005 Pearson Education, Inc.
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
4 4.2 © 2012 Pearson Education, Inc. Vector Spaces NULL SPACES, COLUMN SPACES, AND LINEAR TRANSFORMATIONS.
Math 15 Lecture 10 University of California, Merced Scilab Programming – No. 1.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Basic Control Structures
Introduction to Java Java Translation Program Structure
Linear Algebra Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Working with Arrays in MATLAB
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Linear Algebra Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
MATLAB Programming COMM2M Harry R. Erwin, PhD University of Sunderland.
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Introduction to Computing Systems and Programming Programming.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction Types of Matrices Operations
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
VBA - Excel VBA is Visual Basic for Applications
Chapter - 12 GRAPH MATRICES AND APPLICATIONS.
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Arrays … The Sequel Applications and Extensions
MATLAB Programming Indexing Copyright © Software Carpentry 2011
MATLAB Programming Basics Copyright © Software Carpentry 2011
‘If’ statements, relational operators, and logical operators
Program Flow.
Matrices are identified by their size.
Presentation transcript:

Basic Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See for more information. MATLAB Programming

MATLABBasic Flow Control MATLAB is a programming language Flow control: if – else for while Functions Data parallel approach: don’t use flow control when something else is better.

MATLABBasic Flow Control Example: For m in M: If m < 50: A(m) else: B(m) Or… A (M < 50) B (M<50) These are equivalent.

MATLABBasic Flow Control Disease statistics over time: –One row per patient –Columns are hourly responsive T cell counts –Replace noisy values with local interpolation Time…12:0013:0014:0015:0016:00 Person23…

MATLABBasic Flow Control Time…12:0013:0014:0015:0016:00 Person23… Time…12:0013:0014:0015:0016:00 Person23… ( ) / 2 = Use the two closest data points.

MATLABBasic Flow Control Functions in MATLAB should go in files with the same name and a “.m” extension.

MATLABBasic Flow Control The first line of your function file is the header:

MATLABBasic Flow Control function clean_data = interpolate(data) Naive implementation: For each person For each data value If it the value is 0, replace it with the local interpolate.

MATLABBasic Flow Control MATLAB for loops: >> for i = 1:5 >> i >> end Loop variable

MATLABBasic Flow Control MATLAB for loops: >> for i = 1:5 >> i >> end Loop sequences: a:b Loop between a and b inclusive, jumping by one. a:c:b Loop between a and b inclusive, jumping by c. Loop sequence

MATLABBasic Flow Control MATLAB for loops: >> for i = 1:5 >>i >> end What will this display?

MATLABBasic Flow Control MATLAB for loops: >> for i = 1:5 >>i >> end What will this display?

MATLABBasic Flow Control function clean_data = interpolate(data) [ppl_count, measure_count] = size(data);

MATLABBasic Flow Control function clean_data = interpolate(data) [ppl_count, measure_count] = size(data); clean_data = data;

MATLABBasic Flow Control function clean_data = interpolate(data) [ppl_count, measure_count] = size(data); clean_data = data; for person = 1:ppl_count for measurement = 2:(measure_count-1) if clean_data(person, measurement) < EPS clean_data(person, measurement) = (data(person, measurement – 1) + data(person, measurement + 1))/2; end

MATLABBasic Flow Control if clean_data(person, measurement) < EPS EPS is roughly 2e-16. Why not test whether the value == 0? Tests of equality with floating point numbers are often wrong: 0.0 !=

MATLABBasic Flow Control What about a return statement? When a function ends, the last value assigned to each return variable is returned.

MATLABBasic Flow Control How much work does the program do? for person = 1:ppl_count for measurement = 1:measure_count if clean_data(person, measurement) < EPS … Is this data parallel?

MATLABBasic Flow Control Data Parallel: Let MATLAB identify the locations that are zero and only loop over those locations RowColumn MATLAB function “find non-zeros”

MATLABBasic Flow Control MATLAB’s find function returns all non-zero elements. [I,J] = find(M) # Return locations such that M(I,J) are all nonzero entries. Use “help find” to investigate other parameter and return sets that find provides. How can we use ‘find’ to find zeros rather than nonzeros?

MATLABBasic Flow Control function clean_data = interpolate(data) [I, J] = find( data < EPS ); data < EPS [I,J] = find(…) I J

MATLABBasic Flow Control function clean_data = interpolate(data) [I, J] = find( data(2:end-1) < EPS ); clean_data = data; for i = 1:length(I) person = I(i); measurement = J(i); clean_data(person, measurement) = data(person,measurement-1) + data(person,measurement+1) / 2; end

MATLABBasic Flow Control Have we reduced the amount of work that the computer has to do? Not really: somewhere, a loop is checking for all entries in ‘data’ that are zero. But we are using a library routine, so: It is faster. It has been debugged.

MATLABBasic Flow Control Have we reduced the amount of work that the computer has to do? Not really: somewhere, a loop is checking for all entries in ‘data’ that are zero. But we are using a library routine, so: It is faster. It has been debugged. Can we do even better?

MATLABBasic Flow Control function clean_data=interpolate(data) [I, J] = find( data(2:end-1) < EPS ); clean_data = data; clean_data(I,J) = data(I,J-1) + data(I,J+1) / 2;

MATLABBasic Flow Control MATLAB provides a complete set of flow control structures: While If – else if – else For But you should think carefully before you use them.

MATLABBasic Flow Control Before you write a function: Is there a pre-built function that I can use instead? Use MATLAB’s help function to get a long list of available functions. Before you write a loop or an if statement: Is there a data parallel way to do the same thing?

February 2011 created by Richard T. Guy Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See for more information.