MATLAB Basics Stanley Liang, PhD York University Configure a MATLAB 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 The MATLAB Student package 1
MATLAB User Interface Variables in memory File manager Script editor Data structure in the selected.mat file Interactive command window MATLAB User Interface Provide interactive UI to implement machine learning Comment / uncomment code 2
Basic MATLAB Coding Go to: http://www.machinelearninghellix.site/ Download day one exercise 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 one Storing Data in Variables 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 Built-in Functions & Constants MATLAB constants: pi, i We can use a script to define a set of MATLAB constants (MATLAB class on 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. 3
Vectors and Matrices 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 Creating Evenly-Spaced Vectors 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 Array Creation Functions 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 Save and load variables to a.mat file Save variables in the workspace to a MAT file use import tool 4
Indexing into and Modifying Arrays 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 Values in Arrays 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
Array Operations 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 output 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 Functions 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( ) Obtaining Help 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 own problems Visit mathworks.com 6
plot(x axis, y axis) Plotting Data 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 Logical Operations and Variables 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
Combining Logical Conditions 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) otherwise The operator returns true (1) if either element is true Logical Indexing 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 8