Download presentation
Presentation is loading. Please wait.
Published byNatalie Jones Modified over 9 years ago
1
EGR 115 Introduction to Computing for Engineers MATLAB Basics 3: Array Operations Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers
2
Lecture Outline Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Data files, Array Operations, and Some of the built in functions Slide 2 of 10
3
MATLAB basics Data files: Saving data Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Often as engineers we will need to save the results of an analysis task or load data from a test procedure. MATLAB allows data to be saved from a session >> save filename var1 var2 var3 o A file called “filename.mat” will be created – This is a compact binary data file (not human readable) o Var1 and Var2 refer to variables in the workspace It is also possible to save to an ASCII data file (human readable)ASCII >> save filename.txt var1 var2 -ascii Slide 3 of 10
4
Creates the file “my_data.mat” in your current working directory MATLAB basics Data files: Loading data from files Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers The load function is the complement of save and can retrieved data from a file >> load filename.mat An example: >> a = [1 2 3 4];% variable 1 >> b = [4; 8];% variable 2 >> save my_data a b;% Save variables ‘a’ and ‘b’ >> clear all;% Clear the workspace >> load my_data;% Load the variables from the file o Note that the variables are removed from the workspace and reappear after being loaded!! Slide 4 of 10 Try saving both a and b to “my_data.txt” as an ascii file – Open the file
5
MATLAB basics Array Operations Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Array Addition (c = a + b) Only makes sense if ‘a’ and ‘b’ are the same size An Example: Slide 5 of 10 Col index Row index
6
MATLAB basics Array Operations - Multiplication Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Array Multiplication (c = a * b) Only makes sense if ‘a’ and ‘b’ are compatible o Number of columns of ‘a’ must be the same as the number of rows of ‘b’ – A nXm matrix times a mXr matrix yields a nXr matrix An Example: (a is a 2X3 matrix and ‘b’ a 3X4, thus ‘c’ is 2X4) Slide 6 of 10 What is the result of: >> c = b * a ??? Create two square matrices ‘a’ & ‘b’: Is a* b = b * a ?
7
MATLAB basics Array Operations – Element by Element Multiplication Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Element by Element Array Multiplication (c = a.* b) Only makes sense if ‘a’ and ‘b’ are exactly the same size An Example: (a is a 2X3 matrix and ‘b’ a 2X3, thus ‘c’ is 2X3) Slide 7 of 10
8
MATLAB basics Array Operations – Matrix Division? Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Slide 8 of 10 What is the result of: >> c = inv(b) * a ???
9
MATLAB basics Some of the built in Functions Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Slide 9 of 10
10
MATLAB basics Some of the built in Functions Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Slide 10 of 10 Some of the built in functions
11
Next Lecture Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers Intro to Plotting Slide 11 of 10
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.