Introduction to Matlab

UNIVERSITY OF SHEFFIELD

CiCS DEPARTMENT

Deniz Savas, Mike Griffiths & Research Computing Group

July 2018 Part 1 - Contents • Introducing MATLAB – Supported Platforms – Obtaining/Accessing MATLAB at Sheffield University – MATLAB Windows Layout – Entering Commands • Storing Data into Variables – Command Language Syntax – Types of Data (i.e Variables – Built-in Constants • Working with vectors & matrices – Arrays & Matrices – Array & Operations and Array Addressing – Built-in Functions & help about them • Reading in Data from Files • Other MATLAB Data Types What is MATLAB?

• MATrix LABoratory • State of the Art Scientific Computation and Visualisation Tool, • MATLAB has its own high level programming language, • It can be used as an application builder, • It is extendible via freely or commercially available Toolboxes. Supported Platforms

• Windows • Apple/Mac OSX • Unix/Linux platforms • MATLAB documentation is freely available at: http://uk.mathworks.com/help/index.html

• Other open-source Matlab clones are ; – Octave http://octave.sourceforge.net/ – SciLab http://www.scilab.org/ Obtaining Matlab

• Free for Students and Staff at The University of Sheffield through the Campus Matlab Site License – Via Managed Windows Service – Central HPC service (Iceberg & sharc) • Research staff and research students can download and install the desktop version – http://www.shef.ac.uk/cics/software/slmatlab • Further information at – https://www.sheffield.ac.uk/cics/research/software/matlab Starting Matlab

• On Windows – Load Applications – Start->Programs->Matlab->Matlab_xxx->Matlab_xxx ( where xxx is the version name ) • On the Sheffield HPC clusters iceberg & sharc – Open a secure shell client to iceberg and login – Start an interactive session using, qsh – Type: module load/apps/matlab Notes: • See https://www.sheffield.ac.uk/cics/research/hpc/using/access/intro for details of logging into iceberg and sharc. • See http://docs.iceberg.shef.ac.uk/en/latest/sharc/software/apps/matlab.html for information on running matlab on sharc. Startup Options for Matlab

• Command-line Startup Options – Interactive without display matlab –nodisplay – Don’t display splash on startup matlab –nosplash – Start without Java interface ( GUI's will not work ) matlab -nojvm • Customizing actions to take when starting/stopping – Customise using template files named startupsav.m and finishsav.m in the directory ../toolbox/local The Matlab Desktop - Workspace

• Main Workspace – Command Window – Command History – Current Directory – Variables Workspace • Help Window • Editor and debugging Window • Profiling Window • Graphics Editor Window Directory Navigation Search Help

Workspace General Syntax Rules

• Matlab is case sensitive. • COMMENTS: Any line starting with % is a comment line and not interpreted. Similarly comments can be added to the end of a line by preceding them with the % sign • CONTINUATION LINES: A statement can be continued onto the next line by putting … at the end of the line • More than one statement can be put into a single line by separating them with commas , or semicolons ; . Scalar Variables

• 4.0*atan(1.0) ----> displays result

• pi ----> pi is a built in constant • a = 1.234 ----> define (a) and display result • b = 2.345 ; ---> define (b), but do not display • c = a*b/pi; ---> multiplication and division • d = 1.2+3.4i ----> d is a • e = a+j*b ----> e “ “ • A = 4.55 -----> case is significant (A is not a )

• who or whos get a list of so far defined vars. • Note: Avoid names longer than 31 chars. Built in Scalar variables

• pi • i and j : sqrt(-1) these are used for complex numbers notation • eps : floating point smallest positive non-zero number • realmin : smallest floating point number • realmax : largest floating point number • Inf : infinity • NaN : Not-a-number

It is important to note that these are not reserved words therefore it is possible to re-define their values by mistake. DO NOT RE-DEFINE THESE VARIABLES A scalar variables is really a (1by1) matrix

Matlab is a matrix orientated language. What we can think of as a scalar variable is stored internally simply as a (1 by 1) matrix. Also Matlab matrices can take on complex values if the assignment necessitates it. Practice Session 1

1. Follow 1-Getting Started” instructions on the exercises sheet. 2. Investigate each variable on the Workspace Window

Keep your MATLAB session on, so as to practice new concepts as they are presented. Arrays & Matrices

• r = [ 1 6 9 2 ]  a row vector

• c = [ 3 ; 4 ; 5 ; 7 ]  a column vector

• d = [ 4 5 6 ; 7 8 9 ; 5 3 2; 1 2 3 ] 4by3 matrix

• A= rand(1,5)  1 row of 5 columns containing random numbers.

• Array Operations

• Given r as (1 by n) (row vector) and c as • (n by 1) (column vector). • r*c ------> inner product ( single number ) • c*r ------> a full matrix

• constant*matrix is an array of same • or ------> dimensions where each • matrix*constant element is multiplied • by a constant Array Addressing

• Direct Index Addressing x(3)  reference to 3rd element of x x( [6 1 2] )  6th , 1st and 2nd elements of x array.

• Array Section Referencing (Colon notation) array( first:last) or array(first:increment:last) e.g. x(1:5)  elements 1, 2, 3, 4 and 5 of x x(4:-1:1)  elements 4 , 3 , 2 and 1 of x Array Addressing Continued

• Addressing via an index array

d = [ 11.1 12.2 13.3 14.4 15.5 16.6 ]; e = [ 4 2 6] ; f = d(e) will result in setting f =[ 14.4 12.2 16.6 ] Find Function

• Find returns the indices of the vector that are non-zero. When used with relational operators it will return the indices of a vector satisfying a given condition.

EXAMPLE: ind = find( A > pi ) The Use of find Function

If vector a is a = [ 1.3 5.6 7.8 2.0 4.0 3.8 2.5] then k = find(a < 3.0) will return k=[1 4 7] and c=a(k) will be a new vector made up of the 1st ,4th and 7th elements of a in that order. Examples: find( a > 10 & a <60 ) ; find the indices of all elements of a that lies between 10 and 60. b( find(b< 0.0 | b > 100.0 ) = [] ; find elements of b that is less than 0 or greater than 100 and eliminate them.

MATLAB sometimes assigns the value NaN to variables or elements which could not be represented numerically. NaN can not be used in logical expressions such as a == NaN. To find the elements of a vector of an array that contain NaN use the function isnan. Example: a ( isnan(a) ) = 999.0 will all elements that were set to NaN to 999.0 . Conclusion: Results of the find() function can be used as an index vector to select or eliminate data which meets a certain criteria. Array Constructs

• Explicit : x = [ 0.1 0.5 6.3 3.2 5.6 ]; this creates a row vector or y = [ 0.1 ; 0.5 ; 6.3 ; 3.2 ; 5.6 ] ; this creates a column vector

• Using colon notation to create uniformly spaced vectors : first_value : increment : last_value For example: x = 0 : 0.1 : 5.0

• Via the linspace function: linspace(first_number , last_number , number_of_elements) For example: x = linspace( 1.0 , 20.0 , 10 );

Note: To turn a row vector to column vector or visa versa use the transpose notation ‘ . Example z = y’ ; Saving Data into Files & Reading Them Back

Save command can be used to save data into files that can be read back into MATLAB in subsequent sessions by using the load command. In the tutorials directory you will find a file named mydata.mat. This is a file that was created by using the save command during a previous MATLAB session. Read data from that file into your session by using the load command. Study the variables that are read in and note that there are some missing data. We shall later on come back to this data to demonstrate ways of dealing with missing data values. Save command

Save command is one of those MATLAB commands that can be invoked as a command or a function.

In command form the syntax is : save (optional params) filename ( optional variables ) Examples: save allmydata save –ascii file1.txt a , b , c

Or when it is invoked as a function the syntax is: save( ‘optional parameters’ , ‘filename’ , optional variables’ ) Example: save ( ‘-ascii‘ , ‘-tabs’ , ‘myres.txt’ , ‘a’ , ‘b’ , ‘c’ ) Reading Data into MATLAB

• Usually, you will already have some data that you wish to analyse using MATLAB • MATLAB provides a wealth of functions and also interactive tools to import various formats of data. Some of the formats are: – Matlab’s own format (.mat) files generated by the save command – Text files, – Comma separated text files such as produced by excel (CSV) – Excel spreadsheats ( XLS ) – Various formats of Graphics files, jpeg, png etc … • There are also similar reciprocal functions for writing the data back onto disk in CVS, XLS, JPEG etc. formats. Practice Session 2 Reading in Nicely Formatted Text Files

An example simple text data file named .txt is provided in the exercises directory.

1.Explore the contents of this file (by double clicking on it in the folder window) 2.Use the load command to read the data from this file. 3.Explore the workspace to see what is read. 4.By double clicking on matrix field, view its contents and change its element that reads as NaN to 3.5 by editing it. What does NaN mean ? Note that any missing data had to be stored as NaN and not left blank so as to be able to read it the way we did. You could equally well use some wild number such as 99999 to indicate missing data and then deal with it later. Using the Import Data Wizard

We shall now load the same data using the import data wizard. 1.Clear workspace before you start, by typing clear 2.Click on the Import Data icon on HOME tab the ribbon at the top 3.Select field.txt to open it. You will find that the wizard did not do a good job and columns of data does not look right. 4.While on this window, click on the Column Delimiters drop-down menu and select space as well as tab as delimiters. 5.Click on Delimiter Options and select Treat Multiple Delimiters as One. 6.You should now have the data looking reasonable, Click on Import Selection and close the Wizard Window. Practice Session 3

We shall now attempt to read a more complex text data file. The file is named gunstats.txt and is also in the exercises directory. The data is obtained from United Nations Data Achieves and is about the numbers of homicides and general gun ownership. The head of the file contains explanations about the data it contains, which is very useful for us to understand the data but can make life difficult when trying to read it to an analysis code. Perform the instructions for Practice-3 in the exercises handout. fillmissing function

• fillmissing function is useful for estimating values for the missing observations. Syntax: fillmissing( variable , ‘method’ , …. )

Type doc fillmissing to get information about its use and provide answers to the questions the

Practice Session 4

Examples: Study the script missexample1.m and perform the listed operations on the data that is provided in the mydata.mat file to eliminate the missing values or estimate the missing values by careful use of the fillmissing command. Maths Operations

• Symbols + , - , * , / , \ , ^ • are all matrix operation-symbols. • • Sometimes we need to perform arithmetic ‘array’ rather than matrix operations between the corresponding elements of arrays treating them as sets of numbers rather than matrices. • This can be achieved by using the . (dot) symbol before the operation symbol • such as .* or ./ . Maths Operations continued …

• If c and d are two vectors of same dimensions • then; • e = c.*d defines a vector e of same size ‘as c and d’ with its elements containing the result of the multiplication of the corresponding elements. • e= c./d is the same but division of elements. • e=c.\d (left division) (d divided by c) Matrix Constructs

• Explicit: A =[ 1 2 3;4 5 6 ; 7 8 9] • Constructed from vectors: A = [ a ;b ;c ] where a,b,c are row vectors of same length or column vectors A = [ a b c ] where a,b,c are column vectors of same length or row vectors • Constructed via Colon Notation: A = [(1:3);(4:6);(7:9) ] Matrix Constructs continued ...

• Generated via a standard function call N = 10 ; B = ones(N); N by N matrix of all 1’s ID = eye(N); N by N identity matrix D = magic(7); magic squares matrix E = rand(4,6); 4 by 6 matrix with random distributed elements between 0.0 and 1.0 Matrix Subsections

• Let A be a 4 by 6 full matrix: A’ ------> Transpose of A ( i.e 6 by 4 matrix ) A( end , : ) ------> The last row of A matrix A(: , end-2:end) ------> The last three columns of A A( 1:3 , 2:4 ) -----> subsection with row 1 ,2, 3 and columns 2,3,4. I.e. a 3by 3 matrix. A( 1:3 , : ) a 3 by 6 matrix which is the first 3 row of A matrix ( i.e here ‘:’ means all columns ). A( 4:-1:1 , : ) same size as A but rows are arranged in reverse order.

• QUESTION: • What is A( : , 6:-1:1 ) ? Matrix Assignments

• Examples: Let A be 4 by 6 matrix • B = A(1:4,6:-1:4) ----> B becomes 4 by 3 matrix • A = A’ A is replaced by its transpose. • A = [ B C] B and C must have the same number of rows. • A = [ B ; C] B and C must have the same number of columns Matrix Assignments continued ...

• Examples: Let A be 4 by 6 matrix • A(4,:) = [ 1 2 3 4 5 6 ] ---> redefine 4th row • A(5,:) = [ 5 4 6 7 8 9 ] ---> add a 5th row • A(:,3) = [ 1 ; 2 ; 3 ; 4] ----> redefine 3rd column Practice Session 5

• Perform Exercises 5.1 to 5.6 from the exercises sheet Shape of Matrices

• What is the size of my matrix ? whos variable_name will give details. size function will return the dimensions size (A ) will return the dimensions of matrix A as two integers. example usage; [ m n ] = size ( A) reshape function can be used to re-shape a matrix. I.e. reshape(A,m,n) will reorganize elements of A into a new m-by-n matrix example: B = reshape( A , 4 , 6 ) Matrix Division

• Matrix inversion function is inv( ) • If A is a non-singular square matrix then • A\B means inv(A)*B • A/B means A*inv(B) • Therefore ; • Solution of A*X = B is A\B • Solution of X*A = B is B/A Solution of Linear Equations

A set of n linear equations in n variables can usually yield an exact solution. For example in the case of the example set out below there will be a unique point ( x,y,z ) triplett that will satisfy all the equation. There are however cases where a solution will not exists due to the fact that one or more of the equations is the same as the combination of other equations thus imparting no new information about the relationship of the variables with each other. Such cases are described as singularities. In our discussions we will assume there are no such singularities. • An example linear equation in 3 dimensions: 2x + 3y + z = 17 x + 4y +2z = 22 x + y +5z = 25 Linear Equations

The best way of visualizing the solution of linear equations is by analogy to the geometrical problem of finding the intersection- point of lines (in 2D ) or planes (in 3D) . For equations involving more than 3 dimensions we can then extrapolate the same arguments to finding a point in the multi-dimensional space. For example the set of equations: 5x-2y = 4 and 2x+3y = 13 represent 2 individual lines in 2D space. Unless these two lines are exactly parallel to each other they will intersect at a point. This point of intersection will lie in both lines by definition of intersection, i.e. it will satisfy both the equations. In our case that corresponds to the point (x=2, y=3). There will be no other combination of x and y that will satisfy both the equations at the same time. This is what we mean by a unique solution. Explaining solution of Linear Equations

Moving on from 2D to 3D an equation such as ; 2x -3y + z = 11 represents a plane in 3D space. If we have two such equations we can then imagine that the intersection of these two planes will be a line along which all the points will belong to both planes. And if we have a third equation of a plane then the point where this line intersects the third plane will have the unique property of belonging to all three planes and hence satisfying all three equations, hence the solution ! We can now extrapolate this argument to multi dimensional hyper spaces where intersection of hyper-spaces lead to spaces with one less dimensions,… eventually to planes, lines and finally to a point. Linear Eqn. cont..

• A = [ 2 3 1 ; 1 4 2 ; 1 1 5 ] ; • b = [17 ; 22 ; 25] ; • x = A\b ----> will yield the answer as 2;3;4 • • C=inv(A) • x = C*b will also work. • you may make sure that the A matrix is not ill • conditioned by finding out its determinant • first: det(A) Overdetermined systems

The previous topic dealt with the desirable scenario of being provided with n linear equations involving n variables which can provide a unique solution. What if we have more equations than variables? This is a scenario where it is no longer possible to have a unique solution. For example if we had m equations in n variables, where m>n then presumably we could select only n of these equations to obtain a unique solution but when we substitute this solution to the remaining m-n equations we discover that the solution does not satisfy those equations. So, what do we do ? This is when the maximum likelihood principle comes into play, which amounts to saying that these equations represent observations that were not very accurate. I.e in a 3D case we were not very accurate when measuring the locations of the surfaces. Having assumed that the locations of the surfaces are probabilistic in nature, we then try to find a solution that has the highest probability (i.e. likelihood) of being the correct solution. MATLAB can deal with these scenario very effectively . Example of Overdetermined Systems

From the working directory read the data file mybigdat.mat

This will provide the coefficients matrix A and the constants vector b that represents 90 equations in 40 variables.

The maximum likelihood estimate of the 40 variables can now be obtained by simply typing x = A\b Underdetermined Systems

In a system of linear equations if the number of equations provided (m) is less than the number of variables(n) we have an entirely different problem. m

Perform Practice exercises 5.7 and 5.8 from the exercise sheet about matrix operations Functions -applied to Matrices

• Most of built-in, ‘what looks like scalar’ functions • can also be applied to vectors or matrices; • For example if A is a matrix then sin(A) • will return a new matrix of same dimensions • with its elements being the sin( ) of the • corresponding elements of A. Multiple Outputs from Functions

• The function sin( ) we have seen in the earlier example returned a single output which was a scalar, vector or matrix of same size as the input parameter. This is usually but not always the case with many primitive MATLAB functions • For example size() function will return n number of outputs where (n) is the dimensionality of the input matrix/vector. For example if A was a matrix of 6 by 10 elements then size(A) will return 6 and 10 as its output. You can capture these output in two separate variables as follows; [ n , m ] = size( A) ;

• A function returning n number of outputs can be invoked in a similar manner with n number of output variables in [ ] brackets. [ out1 , out2 , out3 ,….. outn ] = function ( …. ) Obtaining Help about Functions

MATLAB is very rich in providing documentation about its language in general and built in functions in specific. There are various ways of obtaining help and browsing documentation some of which are listed below; 1.Use the Help icon on the commands ribbon, 2.You can type doc subject to search the documentation for a particular subject. Example: doc decomposition 3.If you know the name of a function type doc function_name to get help about a function or, 4.Type name_of_the_function( and pause 5.For quick text based help on command window type: help function Practice Session 5 about Built in Functions

Perform Exercises 6.1 to 6.5 from the exercise sheet Other Matlab Data types

DATA TYPES • Numeric • Complex, • double precision • Single precision • Integer • Boolean ( Logical ) • Character String • Java Classes • User Defined Matlab Classes

ORGANIZATIONAL ATTRIBUTES

• Multi-dimensional Matrices • Sparse Matrices • Cell Arrays • Tables • Structures & Objects Matlab data types Complex double precision matrices

• This is the default storage mode. • A scalar is really a 1x1 matrix with the imaginary part set to zero. • Any expression which evaluates to a complex value returns complex results automatically • The following are two methods of expressing a complex number; – X = complex( 1.2 , 3.4 ) – X = 1.2+3.4i – Warning i and j has this special meaning which will be lost if you name your variables as i and j . So don’t ! • Matlab keeps track of complex vs real issues and does the necessary conversions. Therefore you need not worry about it. Logical Matrices

• Results of relational operations return logical matrices ( with 1’s and 0’s only ) • These matrices are primary useful for program control structures and they can implicitly be used as masks in vectorisation Example : a = rand (1,10); b = rand(1,10); d= 1:10; c = a < b % c is a logical matrix. a(c) = [ ] % eliminate elements where a

• The older method of storing characters in MATLAB has been as character arrays, where by a sequence of characters are stored as if it is a vector with each element containing a single character. Example: subject=‘crayons’ • From 2017 onwards the newer method of storing characters as string types have been introduced which makes handling character arrays easier. Example: material =“elastine” aa • In the above examples: subject(2) would return ‘u’ where as material(2) would signal an error message as strings are not arrays and are stored in their entirety. Character Arrays

• A Character array is simply a 1 by n array of characters. Example: name=‘tommy’ friends = [ ‘tommy’ ; ’billy’ ; ‘tim ‘ ] Note that all items must have the same length by padding them with blanks where necessary. This ensures that the matrix convention is not violated. Where as; friend = [ ‘Alexandra’ , ‘jim’ ] is a valid construct as the resultant structure is simply an array. In this case friend(3) is ‘e’ for example. Character Strings

• Character Strings are defined by quoting them in double quotes ( “ ) . For example: title=“Saturation of salt crystals” ; • String array can also be formed. For example following the above definition, title(2) = “Solution Parameters” ; Multi-dimensional arrays

• These are arrays with more than 2 subscripts • They can also be generated by using one of the following functions; zeros , ones , rand , randn Example: R = zeros ( 10,20,20) A = ones( 10,20) R(:,:,1) = A Structures

• These are Matlab arrays with elements accessed by field names. They are useful for organizing complex data with a known structure. Structures can be nested. Examples: Creating a structure: order.number= 105; order.year=2003; Order.title=‘Smith’; order(2)= struct( ‘number’,207,’year’,2003, … ‘title’,’Jeff Brown’)

Deleting fields: modorder = rmfield( order , ‘year’ ); Cell Arrays

• Cell arrays are Matlab arrays whose individual elements are free to contain different types & classes of data. These elements need not ‘and usually are not’ of same type, size or shape. They are useful for storing data that can not otherwise be organized as arrays or structures. Cell arrays can be nested. Example Create a 4by2 cell matrix. Each cell can contain any type and size of data: C = cell(4,2)

Create (4by1)cell array B and assign values: B = { [1,2] , [ 4 5 ; 5 6] , 0.956 , ‘range values’} Build a (1by5) cell-array M containing 5 elements, one cell at a time: M{1} = 1 ; M{2}= [1 2 3] ; M{3} =[1 2;34] ; M{4} = rand(10,10) ; M{5}=‘title’ ; Practice Session 7 About Structures & Cell Arrays

Perform practice exercises 7 on the exercises sheet. END OF PART-1