matlab_intro.html Page 1 of 5 Date: Tuesday, September 6, 2005

Similar documents
Getting Started with MATLAB

Tutorial 1 : Introduction to MATLAB

CS129: Introduction to Matlab (Code)

Introduction to image processing in Matlab

Basics. Bilkent University. CS554 Computer Vision Pinar Duygulu

Getting started with MATLAB

MATLAB for Image Processing

Octave Tutorial Machine Learning WS 12/13 Umer Khan Information Systems and Machine Learning Lab (ISMLL) University of Hildesheim, Germany

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

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

MATLAB for Image Processing. April 2018 Rod Dockter

Introduction to Matlab

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

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

QUICK INTRODUCTION TO MATLAB PART I

How to learn MATLAB? Some predefined variables

Introduction to Matlab

CSE/Math 485 Matlab Tutorial and Demo

An Introduction to MATLAB II

Introduction to MATLAB

MATLAB Functions and Graphics

Some elements for Matlab programming

Introduction to MatLab. Introduction to MatLab K. Craig 1

Matlab Introduction. Scalar Variables and Arithmetic Operators

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Introduction to MATLAB Practical 1

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

Mathematical Operations with Arrays and Matrices

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

PART 1 PROGRAMMING WITH MATHLAB

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

A very brief Matlab introduction

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

MATLAB Project: Getting Started with MATLAB

A Guide to Using Some Basic MATLAB Functions

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals

EGR 111 Introduction to MATLAB

Introduction to Engineering gii

MATLAB Project: Getting Started with MATLAB

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

Excel R Tips. is used for multiplication. + is used for addition. is used for subtraction. / is used for division

Homework 1 Description CmpE 362 Spring Instructor : Fatih Alagoz Teaching Assistant : Yekta Said Can Due: 3 March, 23:59, sharp

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

1. Register an account on: using your Oxford address

Image Processing CS 6640 : An Introduction to MATLAB Basics Bo Wang and Avantika Vardhan

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

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

Basic MATLAB Intro III

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Mathworks (company that releases Matlab ) documentation website is:

Lecturer: Keyvan Dehmamy

Chapter 2. MATLAB Fundamentals

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

INC151 Electrical Engineering Software Practice. MATLAB Graphics. Dr.Wanchak Lenwari :Control System and Instrumentation Engineering, KMUTT 1

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

selected topic in transportation 2552/2. 1 Prepared by Lect.Weerakaset Suanpaga

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

! The MATLAB language

Getting started with MATLAB

Introduction to Matlab

Computer Project: Getting Started with MATLAB

Dr Richard Greenaway

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

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

Lecture 2: Variables, Vectors and Matrices in MATLAB

Summer 2009 REU: Introduction to Matlab

Introduction. Matlab for Psychologists. Overview. Coding v. button clicking. Hello, nice to meet you. Variables

Introduction to MATLAB programming: Fundamentals

LAB 2 VECTORS AND MATRICES

2 Amazingly Simple Example Suppose we wanted to represent the following matrix 2 itchy = To enter itchy in Matla

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

EE168 Handout #6 Winter Useful MATLAB Tips

2D LINE PLOTS... 1 The plot() Command... 1 Labeling and Annotating Figures... 5 The subplot() Command... 7 The polarplot() Command...

Matlab Tutorial 1: Working with variables, arrays, and plotting

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Digital Image Analysis and Processing CPE

1 Introduction to MATLAB

THE BARE ESSENTIALS OF MATLAB

Lab #1 Revision to MATLAB

MATLAB SUMMARY FOR MATH2070/2970

2.0 MATLAB Fundamentals

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

Introduction to Matlab

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

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

Introduction and MATLAB Basics

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

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

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

HERIOT-WATT UNIVERSITY DEPARTMENT OF COMPUTING AND ELECTRICAL ENGINEERING. B35SD2 Matlab tutorial 1 MATLAB BASICS

Getting Started with Matlab

Introduction to Matlab

Stokes Modelling Workshop

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Learning from Data Introduction to Matlab

Introduction to MATLAB

Fundamentals of MATLAB Usage

Math 2250 MATLAB TUTORIAL Fall 2005

The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development

Transcription:

matlab_intro.html Page 1 of 5 % Introducing Matlab % adapted from Eero Simoncelli (http://www.cns.nyu.edu/~eero) % and Hany Farid (http://www.cs.dartmouth.edu/~farid) % and Serge Belongie (http://www-cse.ucsd.edu/~sjb) % This version by Tim Marks (http://mplab.ucsd.edu/~marks) % (1) Help and basics % The symbol "%" is used in front of a comment. % To get help, type "help" (will give list of help topics) or "help topic" % (e.g., "help plot") % If you don't know the exact name of the topic or command you are looking for, % type "lookfor keyword" (e.g., "lookfor regression") % When writing a long matlab statement that exceeds a single row use... % to continue statement to next row. % When using the command line, a ";" at the end means matlab will not % display the result. If ";" is omitted then matlab will display result. % Use the up-arrow to recall commands without retyping them (and down % arrow to go forward in commands). % Other commands borrowed from emacs and/or tcsh: % C-a moves to beginning of line (C-e for end), C-f moves forward a % character (C-b moves back), C-d deletes a character, C-k deletes % the line to the right of the cursor, C-p goes back through the % command history and C-n goes forward (equivalent to up and down arrows), % tab command completion. % (2) Objects in matlab -- the basic objects in matlab are scalars, % vectors, and matrices... N = 5 % a scalar v = [1 0 0] % a row vector v = [1,2,3] % a row vector v = [1;2;3] % a column vector v = v' % transpose a vector % (row to column or column to row) v = [-4:4] % a vector in a specified range: v = pi*[-4:4]/4 % v = [1:.5:3] % [start:stepsize:end] v = [] % empty vector % Specific matrices: 1ST parameter is ROWS % 2ND parameter is COLS m = zeros(2,3) % a matrix of zeros v = ones(1,3) % a matrix of ones v = rand(3,1) % rand matrix (see also randn) m = eye(3) % identity matrix (3x3) % Create a text file 'matrix_data' containing: % 2 3 4 % 5 6 7 % 1 2 3 % Then type the following commands load matrix_data matrix_data % read data from the file % The variable containing the file data

matlab_intro.html Page 2 of 5 v = [5 6 7]; % access a vector element v(3) % vector(index) length(v) % number of elements in a vector m = [1 2 3; 4 5 6] % a matrix m(1,3) % access a matrix element % matrix(rownumber, columnnumber) m(2,:) % access a matrix row (2nd row) m(:,1) % access a matrix column (1st row) m(:) % turn matrix into a vector (concatenate columns) size(m) size(m,1) size(m,2) % size of a matrix (ROWS,COLS) % number of rows % number of columns m1 = zeros(size(m)) % create a new matrix with size of m who whos % list of variables % list variables with size/type % (3) Simple operations on vectors and matrices %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (A) Pointwise (element by element) Operations: % addition of vectors/matrices and multiplication by a scalar % are done "element by element" a = [1 2 3 4]; % vector 2 * a % scalar multiplication a / 4 % scalar multiplication b = [5 6 7 8]; % vector a + b % pointwise vector addition a - b % pointwise vector subtraction a.^ 2 % pointise vector squaring (note.) a.* b % pointwise vector multiply (note.) a./ b % pointwise vector divide (note.) log( [1 2 3 4] ) round( [1.5 2; 2.2 3.1] ) % pointwise arithmetic operation % pointwise arithmetic operation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % (B) Vector Operations (no for loops needed) % Built-in matlab functions operate on vectors, if a matrix is given, % then the function operates on each column of the matrix a = [1 4 6 3] % vector sum(a) % sum of vector elements mean(a) % mean of vector elements var(a) % variance std(a) % standard deviation max(a) % maximum a = [1 2 3; 4 5 6] % matrix mean(a) % mean of each column max(a) % max of each column max(max(a)) % to obtain max of matrix max(a(:)) % or... %%%%%%%%%%%%%%%%%%%%%%%% % (C) Matrix Operations:

matlab_intro.html Page 3 of 5 [1 2 3] * [4 5 6]' % row vector 1x3 times column vector 3x1 % results in single number, also % known as dot product or inner product [1 2 3]' * [4 5 6] % column vector 3x1 times row vector 1x3 % results in 3x3 matrix, also % known as outer product a = rand(3,2) % 3x2 matrix b = rand(2,4) % 2x4 matrix c = a * b % 3x4 matrix a = [1 2; 3 4; 5 6] % 3 x 2 matrix b = [5 6 7]; % 1 x 3 vector b * a % matrix multiply a' * b' % matrix multiply % (4) Saving your work save mysession save mysession a b clear clear a b load mysession a b % creates file mysession.mat with all variables % save only variables a and b in the file % clear all variables % clear variables a and b % load variables from file % (5) Relations and control statements % Example: given a vector v, create a new vector u with values equal to % v if they are greater than 0, and equal to 0 if they less than or % equal to 0. v = [3 5-2 5-1 0] % 1: USING FOR LOOPS u = zeros( size(v) ); % initialize for i = 1:size(v,2) if( v(i) > 0 ) u(i) = v(i); end end u v = [3 5-2 5-1 0] % 2: WITHOUT FOR LOOPS u2 = zeros( size(v) ); % initialize ind = find( v>0 ) % index into positive elements u2(ind) = v( ind ) % (6) Creating functions using m-files: % Functions in matlab are written in m-files. Create a file called % 'thres.m' In this file put the following: function res = thres( v ) res = zeros( size(v) ); ind = find( v>0 ); res(ind) = v(ind); % initialize % index into positive elements % Save the file thres.m then type the following at the command line:

matlab_intro.html Page 4 of 5 v = [3 5-2 5-1 0] u = thres( v ) % call function from command line % (7) Plotting x = [0 1 2 3 4]; % basic plotting plot( x ); plot( x, 2*x ); axis( [0 8 0 8] ); x = pi*[-24:24]/24; plot( x, sin(x) ); xlabel( 'radians' ); ylabel( 'sin value' ); title( 'dummy' ); gtext( 'put cursor where you want text and press mouse' ); ; subplot( 1,2,1 ); plot( x, sin(x) ); axis square; subplot( 1,2,2 ); plot( x, 2.*cos(x) ); axis square; ; plot( x,sin(x) ); hold on; plot (x, 2.*cos(x), '--' ); legend( 'sin', 'cos' ); hold off; ; m = rand(64,64); imagesc(m) colormap gray; axis image axis off; % multiple functions in separate graphs % multiple functions in single graph % matrices as images gcf (gcf) (1) % Figure number of current (active) % Bring current window to front % Make 1 current and bring it to front close(1) close(gcf) close all % close 1 window % close current window % close all windows % (8) Working with the Images and the Matlab Image Processing Toolbox [I,map]=imread('trees.tif'); imshow(i,map) I2=ind2gray(I,map); imshow(i2) % read a TIFF image % display it as indexed image w/colormap % convert it to grayscale % display image image(i2) % display image less intelligently colormap(gray(256)) % use gray colormap for values 0-255 % 0 = black, 255 = white

matlab_intro.html Page 5 of 5 imagesc(i2,[0 20]) % scale data to use full colormap % for values between 0 and 50 % 0 = black, 50 or greater = white % Now drag the window to stretch the image's aspect ratio. Then type: axis image I=imread('football.jpg'); % make displayed aspect ratio proportional % to image dimensions % read a JPEG image into 3D array imshow(i) rect=getrect; % use mouse to select rectangle. Then: I2=imcrop(I,rect); % crop I2=rgb2gray(I2); % convert to grayscale (2D matrix) max(i2(:)) % maximum intensity value in I2 min(i2(:)) % minimum intensity value in I2 colormap(gray(256)); % use gray colormap for values 0-255 image(i2) % display image imagesc(i2) % scale data to use full colormap % between min and max values in I2 % min = black, max = white colorbar pixval truesize truesize(2*size(i2)) I3=imresize(I2,0.5,'bil'); I3=imrotate(I2,45,'bil','crop'); I3=double(I2); imagesc(i3.^2) imagesc(log(i3)) % turn on color bar % display pixel values interactively % as cursor is positioned over image % display at resolution of one screen pixel % per image pixel % display at resolution of two screen pixels % per image pixel % resize by 50% using bilinear interpolation % rotate 45 degrees and crop to original size % convert from uint8 to double, to allow % math operations % display squared image intensities (pixel-wise) % display log of image intensities