introduction to matlab the language of technical computing

26
Introduction to MATLAB Introduction to MATLAB The language of Technical Computing

Upload: landon-shakespeare

Post on 14-Dec-2015

253 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to MATLAB The language of Technical Computing

Introduction to MATLABIntroduction to MATLABThe language of Technical Computing

Page 2: Introduction to MATLAB The language of Technical Computing

MATLABMATLAB

MATrix LABoratory- Everything is represented by matrices!

It is a program for doing Numerical Computation.

Also used widely as a programming language to develop tools for Machine Learning.

Page 3: Introduction to MATLAB The language of Technical Computing

Why MATLAB?Why MATLAB?Large toolbox of numeric/image

library functions.Very useful for displaying, visualizing

data. High-level: focus on algorithm

structure, not on low-level details. allows quick prototype development

of algorithms.It is an Interpreter, not as fast as compiled code.

Page 4: Introduction to MATLAB The language of Technical Computing

The MATLAB EnvironmentThe MATLAB Environment

Page 5: Introduction to MATLAB The language of Technical Computing

VariablesVariablesMATLAB 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 andcontain only one row OR one column.

Scalars are matrices with only one row AND one column.

Page 6: Introduction to MATLAB The language of Technical Computing

MATLAB ProgrammingMATLAB ProgrammingThe symbol “%” is used to

indicate a Comment.A “;” at the end of the line

implies MATLAB won’t print the output of the statement. Otherwise it will print the output, which is sometimes useful for printing variable values.

Page 7: Introduction to MATLAB The language of Technical Computing

MATLAB ProgrammingMATLAB Programminga = [1,2,3,4]MATLAB Output:a = 1 2 3 4

a=[1,2,3,4]; %Notice the semicolon

MATLAB Output:<Empty>

Page 8: Introduction to MATLAB The language of Technical Computing

Command LineCommand LineMATLAB's command line is like a

standard shell: - Up arrow to recall commands

without retyping and down arrow to go forward.

Opening a new file in editor:>> edit test.m MATLAB source file extension is .m.Running a program : >> test

Page 9: Introduction to MATLAB The language of Technical Computing

MATLAB ProgrammingMATLAB Programminga = [1 2; 3 4]; % Creates a 2x2

matrix The simplest way to create a

matrix is to list its entries in square brackets. The ";" symbol separates rows; the (optional) "," separates columns.

N = 5 % A scalar v = [1 0 0] % A row vector

Page 10: Introduction to MATLAB The language of Technical Computing

MATLAB ProgrammingMATLAB Programmingv = [1; 2; 3] % A column vectorv = v’ Transpose a Vector or Matrix(row

to column and column to row)v = [] Empty Vector

Page 11: Introduction to MATLAB The language of Technical Computing

MATLAB ProgrammingMATLAB Programmingm = zeros(2, 3) Creates a 2x3 matrix of zerosv = ones(1, 3) Creates a 1x3 matrix (row vector) of

onesm = eye(3) %Identity matrix (3x3)v = rand(3, 1) Randomly filled 3x1 matrix (column vector)

Page 12: Introduction to MATLAB The language of Technical Computing

Indexing in MATLABIndexing in MATLAB

REMEBER: Indices always start from “1”, not “0”.

Matrix(ROW#,COLUMN#)m(1,3) %1st row 3rd columnm(2,:) %access whole second

rowm(:,3) %access whole second

column

Page 13: Introduction to MATLAB The language of Technical Computing

OperatorsOperatorsAssignment = a = b (assign b to

a)Addition + a + bSubtraction - a -bMultiplication * or .* a*b or a.*bDivision / or ./ a/b or a./bPower ^ or .^ a^b or a.^bA “.” means element wise operation

Page 14: Introduction to MATLAB The language of Technical Computing

Conditional StructuresConditional Structures

for i=1:2:7 %Loop from 1 to 7, steps of 2

if(i==3)disp(‘i is 3’) %print output

elseif(i==5)disp(‘i is 5’)

end end

Page 15: Introduction to MATLAB The language of Technical Computing

FunctionsFunctions

All functions are separate m-files. The first line in a function file

must be of this form: function [outarg_1, ..., outarg_m] = myfunction(inarg_1, ..., inarg_n)

The function name should be the same as that of the file.

Page 16: Introduction to MATLAB The language of Technical Computing

Function ExampleFunction Examplemyfunction.m

function y = myfunction(x)

a = [-2 -1 0 1]; y = a + x;

Page 17: Introduction to MATLAB The language of Technical Computing

Function ExampleFunction Exampleanotherfunction.m

function [y, z] = anotherfunction(a, b)

y = a + b; z = a - b;

Page 18: Introduction to MATLAB The language of Technical Computing

PlottingPlotting

x=rand(1,100);y=rand(1,100);plot(x,y,’*’);

Page 19: Introduction to MATLAB The language of Technical Computing

PlottingPlottingTo put a label on X-Axisxlabel(‘my x label’);

To put a label on Y-Axisylabel(‘my y label’);

To put a Title of the Plottitle(‘my title’);

Page 20: Introduction to MATLAB The language of Technical Computing

MATLAB Image ProcessingMATLAB Image ProcessingImage Processing Toolbox is

needed.I=imread(‘cute_baby.jpg’); %read

Imageimshow(I) %show image

Page 21: Introduction to MATLAB The language of Technical Computing

MATLAB Image ProcessingMATLAB Image Processing

I2=rgb2gray(I); % convert RGB to gray

imwrite(I2, ‘cute.jpg’); % save gray image

figure, imshow(I2) % image in new figure

Page 22: Introduction to MATLAB The language of Technical Computing

MATLAB Image ProcessingMATLAB Image Processing

figure,imhist(I2) % show histogram

Page 23: Introduction to MATLAB The language of Technical Computing

MATLAB Image ProcessingMATLAB Image ProcessingFrom the histogram, we see that

the image intensity is missing low values, only high values are present.

Page 24: Introduction to MATLAB The language of Technical Computing

MATLAB Image ProcessingMATLAB Image Processing

I3 = histeq(I2); %Histogram Equalization

figure, imhist(I3);

Page 25: Introduction to MATLAB The language of Technical Computing

MATLAB Image ProcessingMATLAB Image Processing

figure, imshow(I2) %Original imagefigure, imshow(I3) % Equalized

Image

Page 26: Introduction to MATLAB The language of Technical Computing

Help with MATLABHelp with MATLABType help at the MATLAB prompt

or help followed by a function name for help on a specific function.

Online documentation for MATLAB at the MathWorks website:

www.mathworks.comThere are also numerous tutorials

online that are easily found with a web search.