Introduction to MATLAB The language of Technical Computing.

Post on 14-Dec-2015

240 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

Transcript

Introduction to MATLABIntroduction to MATLABThe 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.

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.

The MATLAB EnvironmentThe MATLAB Environment

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.

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.

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>

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

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

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

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)

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

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

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

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.

Function ExampleFunction Examplemyfunction.m

function y = myfunction(x)

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

Function ExampleFunction Exampleanotherfunction.m

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

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

PlottingPlotting

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

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’);

MATLAB Image ProcessingMATLAB Image ProcessingImage Processing Toolbox is

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

Imageimshow(I) %show image

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

MATLAB Image ProcessingMATLAB Image Processing

figure,imhist(I2) % show histogram

MATLAB Image ProcessingMATLAB Image ProcessingFrom the histogram, we see that

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

MATLAB Image ProcessingMATLAB Image Processing

I3 = histeq(I2); %Histogram Equalization

figure, imhist(I3);

MATLAB Image ProcessingMATLAB Image Processing

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

Image

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.

top related