Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is Matlab?  “MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB,

Similar presentations


Presentation on theme: "What is Matlab?  “MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB,"— Presentation transcript:

1 What is Matlab?  “MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB, you can analyze data, develop algorithms, and create models and applications. The language, tools, and built-in math functions enable you to explore multiple approaches and reach a solution faster than with spreadsheets or traditional programming languages, such as C/C++ or Java™.  You can use MATLAB for a range of applications, including signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology. More than a million engineers and scientists in industry and academia use MATLAB, the language of technical computing.  The basic data element is the “array”, usually referred to as a vector or matrix

2 learn how to program because…  Increase your freedom  Increase your scientific value  Enjoyment  Exercise your logical mind

3 PROGRAMMING == PROBLEM SOLVING the concept of an ALGORITHM

4 The process of programming  Programming is not a linear process  Lots of trial and error  Problem solving, detective work, deductive reasoning  Debugging may take longer than initial writing. Enjoy it!

5 Coding philosophy  There are many different ways to make things work.  The prettiest way is not always the most desirable.  Top priority is that your script does what you want it to do.  Good coding practices are important  Assume you will remember nothing next time you look at your code  Assume someone else will be using your code  Assume your script will at some point move to another computer

6 Navigating the Matlab Desktop  Command Window  Where you type the commands into Matlab  Command History  Contains a record of all commands entered in the Command Window  Current Folder Browser  All the contents of the Current Folder where you can save your work  Workspace Browser  Where your data and variables are stored in current Matlab session source: The MathWorks Inc

7 Desktop Layout source: The MathWorks Inc

8 Desktop Layout source: The MathWorks Inc

9 The file browser  Moving around through the folder hierarchy  Command line tools for navigation  pwdwhere are we? cdchange directory lslist directory contents.current directory..parent directory

10 The workspace and variable editor  settings variables: x = 3  clearing variables: clear x clear all  saving & loading variables: save data load data.mat

11 Getting help  help function  doc function  lookfor function >> help sin sin Sine of argument in radians. sin(X) is the sine of the elements of X. See also asin, sind. Reference page in Help browser doc sin

12 Scripts  Anything you type into the workspace can also be run from a script  “.m” files are just saved lists of matlab commands  functions

13 The Editor  Comments  Syntax highlighting  Code folding

14 Variable types  double: floating point number like 3.24  integer: no decimal places 345 Numbers

15 Basic calculations in Matlab  Matlab is a powerful graphical calculator Square root Exponential Absolute value source: The MathWorks Inc Functions?

16 Basic calculations in Matlab

17 -each result is stored in its own variable in the workspace and can be used later in the same session -at any time you can clear (some or all) your variables -if no specific variable name is aasigned to the result, the default is ‘ans’ source: The MathWorks Inc

18 Basic calculations in Matlab  Variables are data containers  All variables are arrays  Scalar (1x1)  Vectors (row or column)  Matrices (m x n)  Default data type is double  double = 8 bytes  16 digits of precision source: The MathWorks Inc

19 Basic calculations in Matlab  Creating Variables salary = 900 Variable name Assign operator value Variables are case sensitive ie. thisvar and Thisvar and THISVAR are all different whos = gives a list of the variables in the workspace Matlab uses the first 31 characters of a variable name only source: The MathWorks Inc

20 Basic calculations in Matlab: Variables  Once variables or graphics are created, they remain stored in memory if not explicitly cleared or overwritten.  This is a common source of errors, since old variables may be mixed with new ones.  Clear variables:  clear all % deletes all variables, compiled functions, etc…  clc% clears the Command Window  close all % close all figures Source: HKA & AB slides

21 Basic calculations in Matlab: Variables Syntax errors source: The MathWorks Inc

22 Vectors and matrices  Vectors are like lists a = [1,2,3,4,5]  Matrices are like lists of lists a = [ 1,3,5,7; 2,4,6,8 ]  Matrices can have many dimensions

23 Creating Vectors  Vectors are one dimensional arrays (either column or row vectors) m = 1 2 3 4 5 6 7 8 9 10 11 12 Row vector Column vector source: The MathWorks Inc

24 Creating vectors >> a = [1 2 3 4 5] a = 1 2 3 4 5 >> a = [1,2,3,4,5] a = 1 2 3 4 5 >> a = [1:5] a = 1 2 3 4 5 >> x = [ 1 2 3 ]; y = [ 4 5 6 ]; >> z = [ x y ] z = 1 2 3 4 5 6 >> z = [x ; y]; % this is a matrix

25 Creating Vectors x = a : dx : b first number last number Interval (default is 1) Also: >> x = x’ transpose Example: >> x=[1:3:13] >> x = x’ For output not to appear on screen but be stored in the workspace use semicolon: >> x=[1:3:13]; Can be used to give multiple commands >> a = 1; b = 2; c = 3; source: The MathWorks Inc

26 Creating vectors  Data can be created in a linear sequence using built in Matlab functions  linspace(x1, x2, N)  This function generates N points linearly between X1 and X2.  For example:  >> a = linspace(1,19,10)  a = 1 3 5 7 9 11 13 15 17 19  If N is not defined it defaults to 100

27 Creating Matrices  Matrices are two dimensional arrays  A couple common ways to create matrices: Use square brackets when creating arrays source: The MathWorks Inc

28 Creating matrices >> a = [1 2 3; 4 5 6] a = 1 2 3 4 5 6 >> a = [1 2 3; 4 5 6; 7 8 9] a = 1 2 3 4 5 6 7 8 9

29 Creating matrices >> ones(3) ans = 1 1 1 >> ones(2,3) ans = 1 1 1 >> zeros(3,4) ans = 0 0 0 0 (rows,columns)

30 Creating matrices >> rand(3) ans = 0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575 >> nan(4) ans = NaN NaN NaN NaN NaN = “Not a Number”

31 Creating Matrices Other ways to enter matrices: Loaded from external data files (check save & load) Generated using built-in functions Created with defined functions in M-files Source: HKA & AB slides

32 Describing matrices  size() will tell you the dimensions of a matrix  length() will tell you the length of a vector

33 Accessing elements >> a = [0:4] a = 0 1 2 3 4 >> a(2) ans = 1 >> b = [1 2 3;4 5 6;7 8 9] b = 1 2 3 4 5 6 7 8 9 >> b(2,3) ans = 6 Also try: x = [1:0.1:10]

34 Accessing elements >> b(1:3,1) ans = 1 4 7 >> b(1,:) ans = 1 2 3

35 Accessing elements >> a = [0:4] a = 0 1 2 3 4 >> a(2) ans = 1 >> b = [1,2,3;4,5,6;7,8,9] b = 1 2 3 4 5 6 7 8 9 >> b(2,3) ans = 6

36 Accessing elements >> b(1:3,1) ans = 1 4 7 >> b(1,:) ans = 1 2 3

37 Accessing elements >> a = [1:.5:5]; >> a([1 2 4]) ans = 1.0000 1.5000 2.5000 >> indices = [5 6 7]; >> a(indices) ans = 3.0000 3.5000 4.0000 >> a([5 6 7]) ans = 3.0000 3.5000 4.0000 equivalent

38 Accessing elements >> odds = [1:2:100]; >> odds([26:50, 1:25]) ans = Columns 1 through 14 51 53 55 57 59 61 63 65 67 69 71 73 75 77 Columns 15 through 28 79 81 83 85 87 89 91 93 95 97 99 1 3 5 Columns 29 through 42 7 9 11 13 15 17 19 21 23 25 27 29 31 33 Columns 43 through 50 35 37 39 41 43 45 47 49

39 Accessing elements >> [26:50,1:25] ans = Columns 1 through 14 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Columns 15 through 28 40 41 42 43 44 45 46 47 48 49 50 1 2 3 Columns 29 through 42 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Columns 43 through 50 18 19 20 21 22 23 24 25

40 Manipulating arrays source: The MathWorks Inc

41 Concatenation & Deleting  Concatenation is the process of joining small matrices to make bigger ones. For example: B = [A A+32; A+48 A+16]  To set a single element to zero: A(3,2) = 0 % Note how to address elements!  To delete the third column of B use: B(:,3) = [ ] Source: HKA & AB slides

42 Vector math  Adding a constant to each element in a vector  Adding two vectors >> a = [1 2 3] a = 1 2 3 >> a + 1 ans = 2 3 4 >> b = [5 1 5] b = 5 1 5 >> a + b ans = 6 3 8

43 Vector multiplication  The * sign refers to matrix multiplication: >> a = [1 2 3] a = 1 2 3 >> b = [2 2 4] b = 2 2 4 >> a * b Error using * Inner matrix dimensions must agree. >> b = b' b = 2 2 4 >> a * b ans = 18

44 Vector multiplication  The.* sign refers to element-wise multiplication: >> a = [1 2 3] a = 1 2 3 >> b = [2 2 4] b = 2 2 4 >> a.* b ans = 2 4 12 >> a * 4 ans = 4 8 12 >> a.* 4 ans = 4 8 12

45 Operators  Element-wise operators:.*multiplication./ division.^exponentiation  Many other functions work element-wise, e.g.: >> a = [1 4 9] a = 1 4 9 >> sqrt(a) ans = 1 2 3

46 relational operators  == equal to (distinguish from = which sets a value)  ~= not equal to  > greater than  <less than  >=greater than or equal to  <= less than or equal to

47 >> 1 == 2 ans = 0 >> 1 < 2 ans = 1 >> 1 = 2 1 = 2 | Error: The expression to the left of the equals sign is not a valid target for an assignment. >> x = 5; >> x < 100 ans = 1 0 means FALSE 1 means TRUE

48 Logical operations  & logical AND operation  | logical OR operation (what about XOR?)  ~ logical NOT operation  == logical equality (unlike “=“, which means assign the result of the expression on the right to the variable on the left Examples: a=1; b=-2;  Logic1 = (a==0 & b==0);  Logic2 = (a==0 | b==0);  Logic3 = (a==1 | b==0);  Logic4 = (a==1 & b==-2);  All logical operations work in “vector” mode as well, so for example if a and b are row or column vectors of equal size then (a==b) evaluates to a vector of the same size as a and b, whose value is 1 if the corresponding elements of a and b are equal, and 0 otherwise. e.g. a = [1 2 3 4 5]; b = [5 4 3 2 1]; c = (a==b) whos

49 >> x = 5; y = 1; >> x > 4 & y > 4 ans = 0 >> (x>4) & (y>4) ans = 0 >> (x>4) | (y>4) ans = 1 >> (y>4) ans = 0 >> ~(y>4) ans = 1

50 Finding values within a matrix >> x = rand(1,10) x = Columns 1 through 8 0.7060 0.0318 0.2769 0.0462 0.0971 0.8235 0.6948 0.3171 Columns 9 through 10 0.9502 0.0344 >> find(x>.5) ans = 1 6 7 9

51 Finding values within a matrix >> indicesWhereBig = find(x>.5) indicesWhereBig = 1 6 7 9 >> x(indicesWhereBig) ans = 0.7060 0.8235 0.6948 0.9502 >> x(find(x>.5)) ans = 0.7060 0.8235 0.6948 0.9502

52 Logical indexing >> x>.5 ans = 1 0 0 0 0 1 1 0 1 0 >> vec = ans; >> whos vec Name Size Bytes Class Attributes vec 1x10 10 logical >> x(vec) ans = 0.7060 0.8235 0.6948 0.9502 >> x(x>.5) ans = 0.7060 0.8235 0.6948 0.9502 equivalent to x(find(x>.5))

53 Logical indexing >> newvec = [1 0 0 0 0 1 1 0 1 0] newvec = 1 0 0 0 0 1 1 0 1 0 >> whos newvec Name Size Bytes Class Attributes newvec 1x10 80 double >> x(numvec) Subscript indices must either be real positive integers or logicals. >> numvec = logical(numvec); >> x(numvec) ans = 0.7060 0.8235 0.6948 0.9502

54 Logical indexing >> x = [1:100]; >> x(x<=23) ans = Columns 1 through 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Columns 15 through 23 15 16 17 18 19 20 21 22 23 >> x = [1:10]; >> x(x<5) = 0 x = 0 0 0 0 5 6 7 8 9 10

55 Computing with arrays: Matrix operations For multiplication, Matlab assumes variables are matrices. Therefore size limitations apply, ie. inner dimensions must agree source: The MathWorks Inc

56 Computing with arrays: Array (or Element) operations Place a dot(.) before multiplication or powers to perform element-wise operations. In these cases, matrices must have exact same dimensions. source: The MathWorks Inc

57

58 Q&A: Match the expected outcome to the operators used. >> A.* B >> A * B ? source: The MathWorks Inc

59 Multivariate Data Consider a data set with three variables: Heart rate, Weight and Hours of exercise per week for five observations. The resulting array might look like D = [ 72 134 3.2; 81 201 3.5; 69 156 7.1; 82 148 2.4; 75 170 1.2 ] source: HKA & AB slides

60 Multivariate Data Calculate mean and standard deviation for D mu = mean(D), sigma = std(D) mu = 75.8 161.8 3.48 sigma = 5.6303 25.499 2.2107 For a list of the data analysis functions available in MATLAB, >> help datafun If you have access to the Statistics Toolbox, >> help stats source: HKA & AB slides

61 Exercises  Create a row vector from 0 to 27 with intervals of 3  Create a column vector with the same contents  What are the remainders after dividing this vector by 2?  Create the first 11 ‘perfect squares’  Create a column vector with the elements from 100 to 0  1+1+1/2+1/6+1/24 (order of operations: MDAS)  e to the power of 3 (use help to find the right function)  3cos(  ), sin([0,  /4,  /2, 3  /4,  ])  logarithm of 1000  Create a row vector with the odd numbers between 0 and 50 source: HKA & AB slides

62 ● Create a matrix m that contains two rows of data. The top row are the numbers 4, 5 and 6 and the bottom row 9, 6 and 3. ● Create a matrix n that contains two columns of data. The 1st column are the numbers 1, 3, 5 and 7 and the 2nd column are 2, 4, 6 and 8. ● What is: (1) m.^2 (2) n.^2 (3) n*m (4) m'*n'

63 A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] sum (A) sum the transpose of A sum the elements on the main diagonal (address the elements directly or use the diag() command) Remind you of anything ? help magic ● Create a matrix A that increases in steps of 1 from 3 up to 107 ● Create a matrix B that decreases in steps of 0.5 from 333 down to -10 ● Create a 2d matrix called C of size 100 rows by 100 columns containing only the number 1 ● Create a 2d matrix called C of size 100 rows by 100 columns containing only the number 2 ● Try defining: D = [ 1, 2, 3, 4; 5 6 7 ] What did I do wrong? ● If I define E = [ 10:1 ] what would you expect E to be? Try it. Can you understand what has happened?

64 ● Investigate the Matlab GUI and make sure you know where the different parts can be found. For example, using the GUI: ● What (if any) variables are currently in your workspace? ● What files are in your current directory? ● What was your last but one command? ● Use the commands length and size to check your variables ● Investigate the help available in matlab. Using the help: ● Find the function for standard deviation. Find the function for variance. ● Read the help on the standard deviation and variance functions ● Apply these two functions to the matrix a=[1:10] ● Do you get 3.0277 and 9.1667?

65 Clear the workspace using >> clear all Now define a data variable x as: >> x = [ 10 11 12 ] Predict what you think will happen in each of these cases: (1) >> x(1) (2) >> x(3) (3) >> x(10) (4) >> x[1] Now test your predictions by typing the commands into Matlab. Make sure you clearly understand what is happening in each case.

66 ● Clear the workspace >> clear all ● Create matrix X: >> X = 333:-3:3; ● How many rows and columns does X consist of? ● Using bracket notation, create a new matrix Y from X that has 3 times as many columns as X but is still only 1 row ● Check this is right with: >> size(Y) should return 1 333 ● Using bracket notation, create a new matrix Z from X that is the same no of columns as X but has 3 rows Check this is right with: >> size(Z) should return 3 111

67 ● Create the following 3x3 matrices: >> G = [ 1 2 3; 4 5 6; 7 8 9] >> H = [ 11 12 13; 14 15 16; 17 18 19] ● Now replace column 1 of G with row 3 of H ● Create matrix J: >> J = [ 1:10; 11:20; 21:30 ] ● Remove the last 5 columns of J and save the resultant matrix in a new variable called K ● Remove the first 2 rows of J and save the resultant matrix in a new variable called L ● Create A=[1 2 3; 4 5 6; 7 8 9]; B = [A A+32; A+48 A+16] Find the coordinates and values of the elements of B that are greater than 53


Download ppt "What is Matlab?  “MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB,"

Similar presentations


Ads by Google