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 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 1
6/7/2017 MAT L AB Use r I nte rfa c e Variables in memory Script editor File manager Interactive command window Data structure in the selected .mat file MAT L AB Use r I nte rfa c e • Provide interactive UI to implement machine learning Comment / uncomment code 2
6/7/2017 Ba sic MAT L AB Co ding Go to: http://www.machinelearninghellix.site/ Download “day one exercise” • use the interactive commands • Recall the previous commands by the Up arrow key – The result stored in “ans” • When you enter just a variable • Initiate variable and assign a name, MATLAB returns the value current value of that variable • The equals sign (=) in MATLAB • MATLAB does not use is the assignment operator reference, so changing one – Value goes from the right to the left variable does not affect another • = is NOT the equal sign one • Use the semicolon to suppress the output MAT L AB Built-in F unc tio ns Sto ring Da ta in Va ria b le s & Co nsta nts • A MATLAB variable starts with a • MATLAB constants: pi, i letter and contain only letters, • We can use a script to define a set of numbers, and underscores(_) MATLAB constants (MATLAB class • Use: clear <variable name> to clear on Day 2) the values in the variable • MATLAB contains a wide variety of • Entering clear only will clean all built ‐ in functions, such as abs variable (absolute value), trigonometry functions, and eig (calculate • Use clc to clears the Command eigenvalues) Window • MATLAB uses parentheses to pass inputs to functions, similar to standard mathematical notation. 3
6/7/2017 Cre a ting E ve nly-Spa c e d Ve c to rs a nd Ma tric e s Ve c to rs • All MATLAB variables are arrays: each • use the “:” operator and specify only the variable can contain multiple elements start and end points • A single number, called a scalar, is • The : operator uses a default spacing of actually a 1 ‐ by ‐ 1 array 1. – You can specify your own spacing • When you separate numbers by spaces – Start : spacing : end (or commas), MATLAB combines the numbers into a row vector (1 ‐ by ‐ n) • If you know the number of elements you • When you separate them by semicolons, want in a vector, use the linspace function MATLAB creates a column vector (n ‐ by ‐ – linspace(start,end,# of element) 1) • Use the transpose operator ( ʹ ) to convert • You can combine spaces and semicolons a row vector into a column vector, or to create matrices vise versa • Computation in a matrix • create column vectors in a single command Sa ve a nd lo a d va ria b le s Arra y Cre a tio n F unc tio ns to a .ma t file • MATLAB functions to create commonly • Save variables in the workspace used matrices to a MAT ‐ file • matrices of random numbers: rand • use import tool • 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 4
6/7/2017 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) 5
6/7/2017 Ar r ay Ope r ations • MATLAB is designed to work naturally • The asterisk (*) operator performs matrix multiplication – dot product with arrays • Addition • if you use * to multiply two equally sized vectors, since the inner dimensions – You can add together any two arrays of the do not agree, you will get an error same size message – Do NOT add a row vector to a column vector • The .* operator performs elementwise • multiply or divide all of the elements of an multiplication array by a scalar. • Basic statistical functions in MATLAB can be applied to a vector to produce a single output • MATLAB has functions that perform mathematical operations on an entire vector or array of values in a single command Calling F unc tions Obtaining He lp • size( ) • Enter: doc fcnName – apply to an array to produce a single output – to get information on any MATLAB function variable containing the array size • The MATLAB documentation contains a • max( ) lot of good examples and information – the maximum value of a vector and its that can help you when working on your own problems corresponding index value can be determined using the max function • Visit mathworks.com – 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( ) 6
6/7/2017 Plotting Data Annotating Plots • plot(x ‐ axis, y ‐ axis) • Labels can be added to plots using plot • The plot function accepts an additional argument annotation functions that allows you to specify the color, line style, and marker style using different symbols in single • The input to these functions is a string quotes. • Use: doc plot for more information • Strings in MATLAB are enclosed in • use the “hold on” command to hold the previous single quotes ( ʹ ) plot and add more plots on the same figure • Use the PLOTS menu • 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. L ogic 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. 7
6/7/2017 L ogic al Inde xing Combining L ogic al Conditions • MATLAB contains logical operators • You can use a logical array as an array which combine multiple logical index, in which case MATLAB extracts conditions such as the array elements where the index is true – AND (&) – OR (|) • You can use logical indexing to • The & operator returns true (1) if both reassign values in an array elements are true, and false (0) otherwise • The | operator returns true (1) if either element is true 8
Recommend
More recommend