function [s p] = sumprod (f, g)

Similar documents
Chapter 7: Programming in MATLAB

An Introduction to MATLAB

Digital Image Processing

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Introduction to Matlab

7 Control Structures, Logical Statements

Programming for Experimental Research. Flow Control

MATLIP: MATLAB-Like Language for Image Processing

Introduction to MATLAB LAB 1

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

MATLAB Operators, control flow and scripting. Edited by Péter Vass

Getting started with MATLAB

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

4.0 Programming with MATLAB

Digital Image Processing. Today Outline. Matlab Desktop. Matlab Basics

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators.

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

int16 map is often stored with an indexed image and is automatically loaded with image when using imread

Scientific Computing with MATLAB

Introduction to Matlab

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

The Warhol Language Reference Manual

Introduction to MATLAB

ECE 202 LAB 3 ADVANCED MATLAB

1 Overview of the standard Matlab syntax

C/C++ Programming for Engineers: Matlab Branches and Loops

AN INTRODUCTION TO MATLAB

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

CS227-Scientific Computing. Lecture 3-MATLAB Programming

Computer Vision. Matlab

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

MATLAB TUTORIAL WORKSHEET

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators

Introduction to MATLAB

Introduction to MATLAB

INTRODUCTION TO MATLAB Part2 - Programming UNIVERSITY OF SHEFFIELD. July 2018

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

Matlab Primer. Lecture 02a Optical Sciences 330 Physical Optics II William J. Dallas January 12, 2005

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

CMAT Language - Language Reference Manual COMS 4115

Today s topics. Announcements/Reminders: Characters and strings Review of topics for Test 1

Summary of the Lecture

Introduction and MATLAB Basics

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

MATLAB SUMMARY FOR MATH2070/2970

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28

Unit 3. Operators. School of Science and Technology INTRODUCTION

Introduction to MATLAB. CS534 Fall 2016

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Getting To Know Matlab

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

LAB 2 VECTORS AND MATRICES

Branches, Conditional Statements

Physics 326G Winter Class 6

Programming in MATLAB

Chapter 4: Programming with MATLAB

Introduction to image processing in Matlab

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Full file at C How to Program, 6/e Multiple Choice Test Bank

Computational Mathematics

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

Lecture 7 Tao Wang 1

Digital Image Analysis and Processing CPE

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Matlab and Octave: Quick Introduction and Examples 1 Basics

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CS1114: Matlab Introduction

MATLAB BASICS. M Files. Objectives

CS1114: Matlab Introduction

Lab 4 CSE 7, Spring 2018 This lab is an introduction to using logical and comparison operators in Matlab.

Matlab Tutorial. Yi Gong

Assignment: 1. (Unit-1 Flowchart and Algorithm)

ENGR 1181 MATLAB 09: For Loops 2

CS100R: Matlab Introduction

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Outline. User-based knn Algorithm Basics of Matlab Control Structures Scripts and Functions Help

LECTURE 1. What Is Matlab? Matlab Windows. Help

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction. Like other programming languages, MATLAB has means for modifying the flow of a program

CHAD Language Reference Manual

Ch.5. Loops. (a.k.a. repetition or iteration)

ECE 122. Engineering Problem Solving with Java

Structure Array 1 / 50

CS 221 Lecture. Tuesday, 13 September 2011

Mathematical Operations with Arrays and Matrices

MATLAB An introductory tutorial

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Transcription:

Outline of the Lecture Introduction to M-function programming Matlab Programming Example Relational operators Logical Operators Matlab Flow control structures Introduction to M-function programming M-files: 1. Script: simply execute a series of Matlab. 2. Function: accept arguments and can product outputs. Function m-files components: a) Function definition line. b) H1 line. c) Help text. d) Function n body. e) Comments. a) Function definition form: function [output] = function_name (input) Example: Function to compute the sum and the product of two images The sum image Function name up to 63 long, more than ignored. function [s p] = sumprod (f, g) Keyword The product image Brackets: Must be (multiple output). May be (one output). Without brackets or equal sign (no outputs). Function calling: >> x= imread ('someimage1.tif'); >> y= imread ('someimage2.tif '); Input images

>> [s, p] = sumprod (f, g); b) H1 line: Single comment line that follows the definition line (no blank lines) between them. % SUMPROD computes the sum and product of two input images. H1 line appears when user types >> help function_name H1 provides information about the M-file. c) Help text: Text block follows H1 (without any blank lines in between the two). Help text is used to provide comments and online help for the function, when user type: >> help function_name: Matlab displays all comments lines that appear between the function name and first (executable or blank) line. d) Function body: Matlab code that performs computation. e) Comments. %- symbol used for comments declaration. Operators: Arithmetic: numeric computations Relational: compare operands quantitively. Logic: perform AND, OR and NOT. Arithmetic operators: Matrix arithmetic. Array arithmetic. Important:. (dot) operator is used for array manipulation. >> A*B % matrix multiplication. >> A.*B % array multiplication (A and B must be the same size). >> C = A.*B % C(i,j) = A (i,j) * B (i,j) Addition and subtraction operations are the same for arrays and matrices.. + and.- are not used. Matlab Programming Example Write an M-function, call it improd that multiplies two input images and outputs the product of the images, the maximum and the minimum values of the product, and a normalized product image whose values are in the range [0, 1]. using the Matlab text editor, write the following code 2

Example 6.1 function [p, pmax, pmin, pn] = improd (f, g) % IMPROD computes the product and other parameters of two images. % [ P, PMAX, PMIN, PN] = IMPROD (F, G) output the % element-by-element product of two input images, % F and G, the product maximum and minimum % values, and a normalized product array with values % in the range [0, 1]. The input images must be % of the same size. They can be of class uint8, % uint16, or double. The outputs are of class double. fd = double (f); gd = double (g); p = fd.*gd; pmax = max (p (:)); pmin = min (p (:)); pn = mat2gray (p); Note: the input images were converted to double using the function double instead of im2double, because if the inputs were of type uint8, im2double would convert them to the range [0, 1]. (We want p to contain the products of the original values). Using the function (calling) >> f = [1 2 ; 3 4]; >> g = [1 2 ; 2 1]; >> [p, pmax, pmin, pn] = improd (f, g) p = pmax = 6 pmin = 1 pn =... >> help improd Relational operators RELATIONAL OPERATOR OPERATOR NAME < Less than <= Less than or equal > Greater than >= Greater than or equal == Equal to ~= Not Equal to Example1: Consider the following sequence of inputs and outputs: >> A = [1 2 3; 4 5 6; 7 8 9]; >> B = [0 2 4; 3 5 6; 3 4 9]; Both A and B must be the same size

>>A==B ans = 0 1 0 0 1 1 0 0 1 Operation A==B produces a logical array of the same dimension as A and B with 1s in locations where the corresponding elements of A and B match, and 0s elsewhere. Example2: >> A >= B ans 1 1 0 1 1 1 1 1 1 A >= B produces a logical array with 1s where the elements of A greater than or equal to the corresponding elements of B and 0s elsewhere. If one operand is a scalar matlab tests the scalar against every element of the other operand. Logical operators Logical operators can operate on both logical and numeric data. Matlab treats a logical 1 or nonzero numeric quantity as true, and logical 0 or numeric 0 as false. >> A = [1 2 0 ;0 4 5]; >> B = [1-2 3; 0 1 1]; Operators Logical >> A & B Operator Name ans: & AND 1 1 0 0 1 1 OR ~ NOT Matlab Flow control structures 1. if, else, and elseif Syntax: a) Single if expression b) Multiple if expression1 1 elsif expression2 2 else 3

2. for executes a group of a fixed number of times: for index = start: increment: Nested for for index1 = start1: increment1: 1 for index2 = start2: increments2: 2 additional loop1 3. while Executes a group of an indefinite number of times, based on a specified logical condition. while expression Nested while while expression1 statement1 while expression2 2 additional loop1 4. Break Terminates execution of a for or while loop. 5. Continue Passes control to the next iteration of a for or while loop, skipping any remaining in the body of the loop. 6. switch switch, together with case and otherwise, executes different groups of, deping on a specified value or string. Switch switch-expression case case-expression case {case-expressoin1, case-expression2,...} otherwise 7. return: Causes execution to return to the invoking function. 8. try.. catch Changes flow control if an error is detected during execution. 5