MATLAB Programming Session

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Microsoft® Access® 2010 Training
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Information Technology in Travel, Hospitality and Tourism
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Chapter 1 - An Introduction to Computers and Problem Solving
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
1 An Introduction to IBM SPSS PSY450 Experimental Psychology Dr. Dwight Hennessy.
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Modules, Hierarchy Charts, and Documentation
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Adding Automated Functionality to Office Applications.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
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
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
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.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Chapter 17 Creating a Database.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
Introduction to Programming with RAPTOR
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
WHAT IS A DATABASE? A DATABASE IS A COLLECTION OF DATA RELATED TO A PARTICULAR TOPIC OR PURPOSE OR TO PUT IT SIMPLY A GENERAL PURPOSE CONTAINER FOR STORING.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Object Oriented Database By Ashish Kaul References from Professor Lee’s presentations and the Web.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the concepts of structured.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Decision Making and Branching (cont.)
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Simple algorithms on an array - compute sum and min.
Algorithms and Pseudocode
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithms and Flowcharts
Introduction to Algorithms
Algorithmic complexity: Speed of algorithms
Matlab Training Session 4: Control, Flow and Functions
Functions and Procedures
Other Kinds of Arrays Chapter 11
Pseudocode An Introduction
Unit# 9: Computer Program Development
Functions and Procedures
Algorithm Discovery and Design
Algorithmic complexity: Speed of algorithms
So where is it anyway? Bounding box, median, centroids, and an introduction to algorithm analysis.
Introduction to Algorithms
Spreadsheets, Modelling & Databases
Algorithmic complexity: Speed of algorithms
Pseudocode For Program Design.
3.2 Working with Data Scope of variables 29/07/2019.
Presentation transcript:

MATLAB Programming Session Program development Planning the program Using Pseudo-code Selecting the right data structures General coding procedures Naming a function uniquely The importance of comments Optimizing for speed Vectorizing your code EE465: Introduction to Digital Image Processing

EE465: Introduction to Digital Image Processing Planning the Program Modular programming Break the problem down into a series of smaller independent tasks Implement each task as a separate function When do I decide to implement a task as a new function? Thumb rule: if this task will be performed more than once (e.g., I might want to use this function in other programs) EE465: Introduction to Digital Image Processing

EE465: Introduction to Digital Image Processing Using Pseudo-code For each pixel x {     - We take a window centered in x and size 2t+1 x 2t+1,  A(x,t).    - We take a window centered in x and size 2f+1 x 2f+1, W(x,f).     wmax=0;     For each pixel y in A(x,t)  &&   y different from x  {         - We compute the difference between W(x,f) and W(y,f),   d(x,y).         - We compute the weight from the distance d(x,y),  w(x,y).                 w(x,y) = exp(- d(x,y) / h);         - If   w(x,y) is bigger than wmax then       wmax = w(x,y);         - We compute the average                average + = w(x,y) * u(y);         - We carry the sum of the weights                 totalweight + = w( x, y); } EE465: Introduction to Digital Image Processing

Selecting the Right Data Structure Usually data structure selection is easy, but sometimes it gets tricky Scenario 1: handle data with varying length (e.g., variable length code) Scenario 2: handle a large amount of data (due to memory constraint) Scenario 3: running speed considerations (e.g., uint8 vs. double) EE465: Introduction to Digital Image Processing

General Coding Practices Use descriptive function and variable names to make your code easier to understand Order subfunctions alphabetically in an M-file to make them easier to find Precede each subfunction with a lock of help text describing what that subfunction does Don’t extend lines of code beyond the 80th column Make sure you have saved your function in a directory recognized by MATLAB path setting EE465: Introduction to Digital Image Processing

Naming a Function Uniquely It appears a simple rule; yet it is not easy to obey (we simply can’t remember them all) >which -all <function name> Doesn’t long function time take more time to type? Yes, but you only need to do it once (you can recall the command using up/down arrow key) TAB key can also help you finish the typing automatically EE465: Introduction to Digital Image Processing

The Importance of Comments I can never emphasize its importance enough, even though I often do a poor job on commenting my own MATLAB programs It makes it easier for both you and others to maintain the program Add comments generously, explain each major section and any smaller segments of code that are not obvious Know how to remove or insert commenting % EE465: Introduction to Digital Image Processing

EE465: Introduction to Digital Image Processing Optimizing for Speed How to vectorize your codes? Avoid using for loop as much as you can Functions Used in Vectorizing (see page 58 of programming_tips.pdf) all: True if all elements of a vector are nonzero. any: True if any element of a vector is a nonzero number or is logical 1 (TRUE). Sum/prod/cumsum (more general than sum) end: END can also serve as the last index in an indexing expression. find (you will find it the most useful) squeeze: remove singleton dimensions. permute,/ipermute, reshape, sort, repmat, shiftdim, ndgrid EE465: Introduction to Digital Image Processing

EE465: Introduction to Digital Image Processing Vectorize Your Code Even if loop is inevitable, try to reduce the number of iterations Example: histogram calculation for a given grayscale image sized 512-by-512 Scheme 1: loop over row and column indexes (512×512=262144 iterations) Scheme 2: loop over intensity values 0-255 (256 iterations) Scheme 2 is better! EE465: Introduction to Digital Image Processing

MATLAB Programming Tip #1 Use functions provided by MATLAB Although we can implement any function from the scratch, the ones offered by MATLAB are optimized in terms of efficiency and robustness. Examples: use bwdist in our implementation of finding skeleton EE465: Introduction to Digital Image Processing

MATLAB Programming Tip #2 Use as few loops as possible Example: implementation of histogram calculation Scheme 1: loop over every position in the image, i.e., i=1-M, j=1-N Scheme 2: loop over every intensity value, i.e., 0-255 EE465: Introduction to Digital Image Processing

MATLAB Programming Tip #3 Parallelism is preferred by MATLAB Example: in finding skeleton, we can find the local maximum in a parallel fashion, which is much more efficient than looping over every pixel % find the local maximum n=[0 1;-1 0;0 -1;1 0]; sk=dt>0; for i=1:4 sk=sk&(dt>=circshift(dt,n(i,:))); end EE465: Introduction to Digital Image Processing