MAT L AB Basic s Stanley Liang, PhD York University Co nfig ure a - - PDF document

mat l ab basic s
SMART_READER_LITE
LIVE PREVIEW

MAT L AB Basic s Stanley Liang, PhD York University Co nfig ure a - - PDF document

6/7/2017 MAT L AB Basic s Stanley Liang, PhD York University Co nfig ure a MAT L AB Pa c ka g e The MATLAB Student package Get a MATLAB Student License on Matworks Visit MathWorks at https://www.mathworks.com/ It is recommended


slide-1
SLIDE 1

6/7/2017 1

MAT L AB Basic s

Stanley Liang, PhD York University

Co nfig ure a MAT L AB Pa c ka g e

Get a MATLAB Student License on Matworks

  • Visit MathWorks at

https://www.mathworks.com/

  • It is recommended signing up with a

student email

  • The MATLAB Student package

– MATLAB and Simulink Student Suite – Image Processing Toolbox – Statistics and Machine Learning Toolbox – Data Acquisition Toolbox

The MATLAB Student package

slide-2
SLIDE 2

6/7/2017 2

MAT L AB Use r I nte rfa c e

File manager

Data structure in the selected .mat file

Script editor Interactive command window

Variables in memory

  • Provide interactive UI to implement machine learning

MAT L AB Use r I nte rfa c e

Comment / uncomment code

slide-3
SLIDE 3

6/7/2017 3

Ba sic MAT L AB Co ding

  • use the interactive commands

– The result stored in “ans”

  • Initiate variable and assign a

value

  • The equals sign (=) in MATLAB

is the assignment operator

– Value goes from the right to the left

  • = is NOT the equal sign
  • Use the semicolon to suppress

the output

  • Recall the previous commands

by the Up arrow key

  • When you enter just a variable

name, MATLAB returns the current value of that variable

  • MATLAB does not use

reference, so changing one variable does not affect another

  • ne

Go to: http://www.machinelearninghellix.site/ Download “day one exercise”

Sto ring Da ta in Va ria b le s

  • A MATLAB variable starts with a

letter and contain only letters, numbers, and underscores(_)

  • Use: clear <variable name> to clear

the values in the variable

  • Entering clear only will clean all

variable

  • Use clc to clears the Command

Window

  • MATLAB constants: pi, i
  • We can use a script to define a set of

MATLAB constants (MATLAB class

  • n Day 2)
  • MATLAB contains a wide variety of

built‐in functions, such as abs (absolute value), trigonometry functions, and eig (calculate eigenvalues)

  • MATLAB uses parentheses to pass

inputs to functions, similar to standard mathematical notation.

MAT L AB Built-in F unc tio ns & Co nsta nts

slide-4
SLIDE 4

6/7/2017 4

Ve c to rs a nd Ma tric e s

  • All MATLAB variables are arrays: each

variable can contain multiple elements

  • A single number, called a scalar, is

actually a 1‐by‐1 array

  • When you separate numbers by spaces

(or commas), MATLAB combines the numbers into a row vector (1‐by‐n)

  • When you separate them by semicolons,

MATLAB creates a column vector (n‐by‐ 1)

  • You can combine spaces and semicolons

to create matrices

  • Computation in a matrix
  • use the “:” operator and specify only the

start and end points

  • The : operator uses a default spacing of

1.

– You can specify your own spacing – Start : spacing : end

  • If you know the number of elements you

want in a vector, use the linspace function

– linspace(start,end,# of element)

  • Use the transpose operator (ʹ) to convert

a row vector into a column vector, or vise versa

  • create column vectors in a single

command

Cre a ting E ve nly-Spa c e d Ve c to rs

Arra y Cre a tio n F unc tio ns

  • MATLAB functions to create commonly

used matrices

  • matrices of random numbers: rand
  • a matrix of all zeros: zeros
  • A single number, called a scalar, is actually

a 1-by-1 array

  • When you separate numbers by spaces (or

commas), MATLAB combines the numbers into a row vector (1-by-n)

  • When you separate them by semicolons,

MATLAB creates a column vector (n-by-1)

  • You can combine spaces and semicolons to

create matrices

  • Computation in a matrix

Sa ve a nd lo a d va ria b le s to a .ma t file

  • Save variables in the workspace

to a MAT‐file

  • use import tool
slide-5
SLIDE 5

6/7/2017 5

Inde xing into and Modifying Ar r ays

  • extract values from an array using row, column indexing

– Var = mat(row, col)

  • use the MATLAB keyword ‘end’ to refer the last element

– Var = mat(end,2)

  • use arithmetic with the keyword end

– Var = mat(end‐1,end‐2) – the 2nd last row and the 3rd last column

  • To extract multiple elements, use the colon operator (:) specifies all the elements in

that dimension.

– x = A(2,:) – A row vector containing all of the elements from the second row of A

  • The colon operator can refer to a range
  • Use a single index to reference vector elements

Changing Value s in Ar r ays

  • use the colon ( : ) character to extract entire

columns of data

–The first row of a matrix: mat(1, end) –The lost column of a matrix: mat(:, end) –A single element with known index: mat(2, 3)

slide-6
SLIDE 6

6/7/2017 6

Ar r ay Ope r ations

  • MATLAB is designed to work naturally

with arrays

  • Addition

– You can add together any two arrays of the same size – Do NOT add a row vector to a column vector

  • multiply or divide all of the elements of an

array by a scalar.

  • Basic statistical functions in MATLAB can

be applied to a vector to produce a single

  • utput
  • MATLAB has functions that perform

mathematical operations on an entire vector or array of values in a single command

  • The asterisk (*) operator performs matrix

multiplication – dot product

  • if you use * to multiply two equally

sized vectors, since the inner dimensions do not agree, you will get an error message

  • The .* operator performs elementwise

multiplication

Calling F unc tions

  • size( )

– apply to an array to produce a single output variable containing the array size

  • max( )

– the maximum value of a vector and its corresponding index value can be determined using the max function – the first output from the max function is the maximum value of the input vector – the second output is the index value – if the input is a matrix, the max function will do column‐wise comparison

  • min( )
  • mean( )
  • Enter: doc fcnName

– to get information on any MATLAB function

  • The MATLAB documentation contains a

lot of good examples and information that can help you when working on your

  • wn problems
  • Visit mathworks.com

Obtaining He lp

slide-7
SLIDE 7

6/7/2017 7

Plotting Data

  • plot(x‐axis, y‐axis)
  • The plot function accepts an additional argument

that allows you to specify the color, line style, and marker style using different symbols in single quotes.

  • Use: doc plot for more information
  • use the “hold on” command to hold the previous

plot and add more plots on the same figure

  • use the hold off command to return to the default

behavior

  • Use “close all” to close all figure windows
  • When you plot a single vector by itself, MATLAB

uses the vector values as

  • % the y‐axis data and sets the x‐axis data to range

from 1 to n

  • The plot function accepts optional additional

inputs consisting of a property name and an associated value.

Annotating Plots

  • Labels can be added to plots using plot

annotation functions

  • The input to these functions is a string
  • Strings in MATLAB are enclosed in

single quotes (ʹ)

  • Use the PLOTS menu

L

  • gic al Ope r

ations and Var iable s

  • Relational operators such as >, <, ==, and ~= perform comparisons between two
  • values. The outcome of a comparison for equality or inequality is either 1 (true) or 0

(false).

  • Note that in MATLAB Not equal is: ~=, Do NOT use: !=, <>
  • You can compare a vector or matrix to a single scalar value using relational operators
  • The result is a logical array of the same size as the original array
  • Corresponding elements of two arrays can be compared using relational operators.

The two arrays must be the same size and the result is a logical array of the same size.

slide-8
SLIDE 8

6/7/2017 8

Combining L

  • gic al Conditions
  • You can use a logical array as an array

index, in which case MATLAB extracts the array elements where the index is true

  • You can use logical indexing to

reassign values in an array

  • MATLAB contains logical operators

which combine multiple logical conditions such as

– AND (&) – OR (|)

  • The & operator returns true (1) if both

elements are true, and false (0)

  • therwise
  • The | operator returns true (1) if

either element is true L

  • gic al Inde xing