Introduzione a MatLab. Prof. Sebastiano Battiato

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

An Introduction to MATLAB II

Getting started with MATLAB

Introduction to MATLAB

A Quick Tutorial on MATLAB. Zeeshan Ali

Lecturer: Keyvan Dehmamy

Quick introduction to Matlab. Edited by Michele Schiavinato

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

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

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

Introduction to MATLAB

Quick introduction to Matlab. PASCAL Bootcamp in Machine Learning 2007 Edited by Luca Cosmo

Introduction to MATLAB

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Introduction to MATLAB

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Stokes Modelling Workshop

Introduction to Matlab

Some elements for Matlab programming

EGR 111 Introduction to MATLAB

Digital Image Analysis and Processing CPE

1 Introduction to MATLAB

Finding, Starting and Using Matlab

A Guide to Using Some Basic MATLAB Functions

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

1 Introduction to MATLAB

What is MATLAB? It is a high-level programming language. for numerical computations for symbolic computations for scientific visualizations

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

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

Introduction to Engineering gii

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Introduction to MATLAB

What is Matlab? A software environment for interactive numerical computations

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Lecture 2 Introduction to MATLAB. Dr.Tony Cahill

Why use MATLAB? Mathematcal computations. Used a lot for problem solving. Statistical Analysis (e.g., mean, min) Visualisation (1D-3D)

Laboratory 1 Octave Tutorial

MATLAB BASICS. M Files. Objectives

Introduction to MATLAB

A Very Brief Introduction to Matlab

Introduction to MATLAB LAB 1

Dr Richard Greenaway

the Enter or Return key. To perform a simple computations type a command and next press the

Edward Neuman Department of Mathematics Southern Illinois University at Carbondale

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

University of Alberta

Matlab Tutorial and Exercises for COMP61021

Desktop Command window

YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM

Introduction to MATLAB

Matlab Lecture 1 - Introduction to MATLAB. Five Parts of Matlab. Entering Matrices (2) - Method 1:Direct entry. Entering Matrices (1) - Magic Square

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

WHAT IS MATLAB?... 1 STARTING MATLAB & USING THE COMMAND LINE... 1 BASIC ARITHMETIC OPERATIONS... 5 ORDER OF OPERATIONS... 7

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

Matlab Tutorial: Basics

CSE/Math 485 Matlab Tutorial and Demo

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

! The MATLAB language

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Chapter 2. MATLAB Fundamentals

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

An Introduction to MATLAB See Chapter 1 of Gilat

A very brief Matlab introduction

Matlab Tutorial, CDS

How to learn MATLAB? Some predefined variables

AN INTRODUCTION TO MATLAB

2.0 MATLAB Fundamentals

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

ECE Lesson Plan - Class 1 Fall, 2001

PART 1 PROGRAMMING WITH MATHLAB

CM0340 Tutorial 2: More MATLAB

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Grace days can not be used for this assignment

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

Matlab Tutorial for COMP24111 (includes exercise 1)

Nuts & Bolts guide to MATLAB

Introduction to MatLab. Introduction to MatLab K. Craig 1

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

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

What is MATLAB and howtostart it up?

A Brief Introduction to MATLAB

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

Introduction to MATLAB

Introduction to MATLAB

Intro to Matlab for GEOL 1520: Ocean Circulation and Climate or, Notions for the Motions of the Oceans

Chapter 1 Introduction to MATLAB

Computer Programming in MATLAB

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

McTutorial: A MATLAB Tutorial

Introduction to MATLAB

Lecture #3. MATLAB image processing (cont.) Histograms Mathematics of image processing Geometric transforms Image Warping.

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Transcription:

Introduzione a MatLab Prof. Sebastiano Battiato

MatLab Environment

MATLAB Matlab = Matrix Laboratory Originally a user interface for numerical linear algebra routines (Lapak/Linpak) Commercialized 1984 by The Mathworks Since then heavily extended (defacto-standard) Alternatives Complements Matrix-X Maple (symbolic) Octave(free; GNU) Mathematica (symbolic) Lyme (free; Palm)

MATLAB The MATLAB environment is command oriented somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears. If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt. The following slide is the text from a MATLAB screen.

MATLAB To get started, type one of these commands: helpwin, helpdesk, or demo» a=5;» b=a/2 b =» 2.5000

MATLAB Variable Names Variable names ARE case sensitive Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer) Variable names must start with a letter followed by letters, digits, and underscores.

MATLAB Special Variables ans Default variable name for results pi Value of π inf NaN Not a numbere.g. 0/0 i (and j) i = j = 1 eps Smallest incremental number realmin The smallest usable positive real number realmax The largest usable positive real number

MATLAB Math & Assignment Operators Power ^ or.^ a^b or a.^b Multiplication * or.* a*b or a.*b Division / or./ a/b or a./b or \ or.\ b\a or b.\a NOTE: 56/8 = 8\56 - (unary) + (unary) Addition + a + b Subtraction - a - b Assignment = a = b (assign b to a)

Other MATLAB symbols >> prompt... continue statement on next line, separate statements and data % start comment which ends at end of line ; (1) suppress output (2) used as a row separator in a matrix : specify range

MATLAB Help System Search for appropriate function >> lookfor keyword Rapid help with syntax and function definition >> help function An advanced hyperlinked help system is launched by >> helpdesk Complete manuals as PDF files

MATLAB Matrices MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an array, in fact, that is how it is stored. Vectors are special forms of matrices and contain only one row OR one column. Scalars are matrices with only one row AND one column

MATLAB Matrices A matrix with only one row AND one column is a scalar. A scalar can be created in MATLAB as follows:» a_value=23 a_value = 23

MATLAB Matrices A matrix with only one row is called a row vector. A row vector can be created in MATLAB as follows (note the commas):» rowvec = [12, 14, 63] rowvec = 12 14 63

MATLAB Matrices A matrix with only one column is called a column vector. A column vector can be created in MATLAB as follows (note the semicolons):» colvec = [13 ; 45 ; -2] colvec = 13 45-2

MATLAB Matrices A matrix can be created in MATLAB as follows (note the commas AND semicolons):» matrix = [1, 2, 3 ; 4, 5,6 ; 7, 8, 9] matrix = 1 2 3 4 5 6 7 8 9

Extracting a Sub-Matrix A portion of a matrix can be extracted and stored in a smaller matrix by specifying the names of both matrices and the rows and columns to extract. The syntax is: sub_matrix = matrix ( r1 : r2, c1 : c2 ) ; where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make the new matrix.

MATLAB Matrices A column vector can be extracted from a matrix. As an example we create a matrix below:» matrix=[1,2,3;4,5,6;7,8,9] matrix = 1 2 3 4 5 6 7 8 9 Here we extract column 2 of the matrix and make a column vector:» col_two=matrix( :, 2) col_two = 2 5 8

MATLAB Matrices A row vector can be extracted from a matrix. As an example we create a matrix below:» matrix=[1,2,3;4,5,6;7,8,9] matrix = 1 2 3 4 5 6 7 8 9 Here we extract row 2 of the matrix and make a row vector. Note that the 2:2 specifies the second row and the 1:3 specifies which columns of the row.» rowvec=matrix(2 : 2, 1 : 3) rowvec = 4 5 6

Reading Data from files MATLAB supports reading an entire file and creating a matrix of the data with one statement. >> load mydata.dat; % loads file into matrix. % The matrix may be a scalar, a vector, or a % matrix with multiple rows and columns. The % matrix will be named mydata. >> size (mydata) % size will return the number % of rows and number of % columns in the matrix >> length (myvector) %length will return the total % no. of elements in myvector

Plotting with MATLAB MATLAB will plot one vector vs. another. The first one will be treated as the abscissa (or x) vector and the second as the ordinate (or y) vector. The vectors have to be the same length. MATLAB will also plot a vector vs. its own index. The index will be treated as the abscissa vector. Given a vector time and a vector dist we could say: >> plot (time, dist) %plotting versus time >> plot (dist) %plotting versus index

Plotting with MATLAB There are commands in MATLAB to "annotate" a plot to put on axis labels, titles, and legends. For example: >> % To put a label on the axes we would use: >> xlabel ('X-axis label') >> ylabel ('Y-axis label') >> % To put a title on the plot, we woulduse: >> title ('Title of my plot')

Plotting with MATLAB Vectors may be extracted from matrices. Normally, we wish to plot one column vs. another. If we have a matrix mydata with two columns, we can obtain the columns as a vectors with the assignments as follows: >> first_vector=mydata(:,1); % First column >> second_vector=mydata(:,2); % Second one >> % and we can plot the data >> plot ( first_vector, second_vector )

Some Useful MATLAB commands who List known variables whos List known variables plus their size help Ex: >> help sqrt Help on using sqrt lookfor Ex: >> lookfor sqrt Search for keyword sqrt in m-files what Ex: >> what a: List MATLAB files in a: clear Clear all variables from work space clear x y Clear variables x and y from work space clc Clear the command window

Some Useful MATLAB commands what dir lssame as dir type test delete test cd a: chdir a: pwd which test List all m-files in current directory List all files in current directory Display test.m in command window Delete test.m Change directory to a: Same as cd Show current directory Display current directory path to test.m

MATLAB Relational Operators MATLAB supports six relational operators. Less Than < Less Than or Equal <= Greater Than > Greater Than or Equal >= Equal To == Not Equal To ~=

MATLAB Logical Operators MATLAB supports three logical operators. not ~ % highest precedence and & % equal precedence with or or % equal precedence with and

MATLAB Logical Functions MATLAB also supports some logical functions. xor (exclusive or) Ex: xor (a, b) Where a and b are logical expressions. The xor operator evaluates to true if and only if one expression is true and the other is false. True is returned as 1, false as 0. any (x) returns 1 if any element of x is nonzero all (x) returns 1 if all elements of x are nonzero isnan (x) returns 1 at each NaN in x isinf (x) returns 1 at each infinity in x finite (x) returns 1 at each finite value in x

Matlab Selection Structures An if - elseif - else structure in MATLAB. Note that elseif is one word. if expression1 % is true % execute these commands elseif expression2% is true % execute these commands else % the default % execute these commands end

MATLAB Repetition Structures A for loop in MATLAB for x = array for x = 1: 0.5 : 10 % execute these commands end A while loop in MATLAB while expression while x <= 10 % execute these commands end

Scalar - Matrix Addition» a=3;» b=[1, 2, 3;4, 5, 6] b = 1 2 3 4 5 6» c= b+a % Add a to each element of b c = 4 5 6 7 8 9

Scalar - Matrix Subtraction» a=3;» b=[1, 2, 3;4, 5, 6] b = 1 2 3 4 5 6» c = b - a %Subtract a from each element of b c = -2-1 0 1 2 3

Scalar - Matrix Multiplication» a=3;» b=[1, 2, 3; 4, 5, 6] b = 1 2 3 4 5 6» c = a * b % Multiply each element of b by a c = 3 6 9 12 15 18

Scalar - Matrix Division» a=3;» b=[1, 2, 3; 4, 5, 6] b = 1 2 3 4 5 6» c = b / a % Divide each element of b by a c = 0.3333 0.6667 1.0000 1.3333 1.6667 2.0000

MATLAB Toolbox MATLAB has a number of add-on software modules, called toolbox, that perform more specialized computations. More than 60 toolboxes!

Image Processing & Matlab

>> clear >> imread('citta.jpg'); >> subplot(2,2,1); >> f=imread('citta.jpg'); >> imshow(f); >> flipped=f(end:-1:1, :, :); >> subplot(2,2,2); >> imshow(flipped); >> cropped=f(60:300, 60:420,:); >> subplot(2,2,3); >> imshow(cropped); >> subplot(2,2,4); >> plot(f(:,10,1));

Tips and Tricks Code Optimization Preallocating Arrays: zeros(m,n) Vectorizing Loops (1D): The matlab code: x=0:m-1 f=a*sin(x/2*pi)); Is better/faster than for x=1:m f(x)=a*sin((x-1/(2*pi)) end;

Tips and Tricks Code Optimization Vectorizing Loops (2D): It is possible to use the function meshgrid,which has the syntax: [C,R]=meshgrid(c,r) The function transforms the domain specified by row vectors c and r into arrays C and R that can be used for the evaluation of functions of two variables. The rows of the output array C are copies of the vector c and the columns of the output array R are copies of the vector r.

Tips and Tricks >> clear >> c=[0,1]; % y = 0,1 >> r=[0,1,2]; % x = 0,1,2 >> [C,R]=meshgrid(c,r); >> C C = 0 1 0 1 0 1 >> R R = 0 0 1 1 2 2 >> h=r.^2+c.^2 h = 0 1 1 2 4 5 >>

Example 1 Compute: f(x,y)=a sin(u 0 x + v 0 y) function [ratio_time,f,g]=twodsinnew(a,u0,v0,m,n) tic %Start timing for r=1:m u0_x=u0*(r-1); for c=1:n v0_y=v0*(c-1); f(r,c)=a*sin(u0_x + v0_y); end; end; t1=toc; %End Timing

Example 1 tic r=0:m-1; c=0:n-1; [C,R]=meshgrid(c,r); g=a*sin(u0*r+v0*c); t2=toc; %End timing ratio_time= t1/(t2+eps) % Eps in case of t2=0

Example 2: Color Transform The human eye is more sensitive to luminance than to chrominance. Typically JPEG throw out 3 / 4 of the chrominance information before any other compression takes place. This reduces the amount of information to be stored about the image by 1 / 2. With all three components fully stored, 4 pixels needs 3 x 4 = 12 component values. If 3 / 4 of two components are discarded we need 1 x 4 + 2 x 1 = 6 values. B Y Cr R G Cb Example

RGB vs YCbCr

RGB conversion & subsampling RGB CMY RGB YCbCr Subsampling 4:4:4(no subsampling) 4:2:2(Cb, Cr horizontal subsampling) 4:2:0(Cb, Cr horizontal + vertical subsampling) = B G R C C Y r b 0.311 0.523 0.212 0.321 0.275 0.596 0.114 0.587 0.299 = B G R Y M C 1 1 1

Matlab a=imread('kodim23.png'); figure;imshow(a); [M,N,I]=size(a) b=uint8(zeros(m,n,i)); RED=a(:,:,1); GREEN=a(:,:,2); BLUE=a(:,:,3); b(:,:,1)=blue; b(:,:,2)=red; b(:,:,3)=green; b=uint8(b);figure;imshow(b); a_reduced=imresize(a,0.3,'bilinear'); a_zoomed1=imresize(a_reduced,2,'nearest'); a_zoomed2=imresize(a_reduced,2,'bilinear'); a_zoomed3=imresize(a_reduced,2,'bicubic'); figure;imshow(a);figure;imshow(a_zoomed1); figure;imshow(a_zoomed2); figure;imshow(a_zoomed3); a_gray=rgb2gray(a); imshow(a_gray); a_ycbcr=rgb2ycbcr(a); Y=a_YCbCr(:,:,1); Cb=a_YCbCr(:,:,2); Cr=a_YCbCr(:,:,3);

Matlab Code (2/2) imshow(y) figure;imshow(cb) Cb_reduced=imresize(Cb,.5,'bicubic'); Cr_reduced=imresize(Cr,.5,'bicubic'); figure;imshow(cb_reduced); Cb_enlarged=imresize(Cb_reduced,2,'bicubic'); Cr_enlarged=imresize(Cr_reduced,2,'bicubic'); a_new_ycbcr(:,:,1)=y; a_new_ycbcr(:,:,2)=cb_enlarged; a_new_ycbcr(:,:,3)=cr_enlarged; a_rgb=ycbcr2rgb(a_new_ycbcr); figure;imshow(a_rgb); figure;imshow(a); Diff=mean(abs(a(:)-a_rgb(:)))

Tutorials on Web http://www.imageprocessingplace.com/root_files_v3/tutorials.htm See also: Chapters 1-2 Digital Image Processing using Matlab