Class 1
Class 1
MATLAB Basics
Matrices and Matrix Operations
Shi, Yining
September 6, 2016
Today’s Class
What is Matlab?
Outline of Course
Teaching materials
Administrative Details
I Class Materials
I Create a directory H://MATLAB_Course in your H: drive
I Hub Website: https://www.imperialbusiness.school
I Download the MATLAB file from the HUB.
I Save the file to H://MATLAB_Course//Class 1 (Class 2 etc )
I We will populate this directory in the following weeks.
MATLAB Layout
I Command Window
I Type commands and display no-graphic output
I A >> prompt shows the system is ready for input
I Current Directory (CD)
I The directory (folder) that MATLAB is currently working in
I Always change the CD to where your MATLAB files are located
I cd('H:/MATLAB_Course/Class 1') for today
I Editor
I The window where you edit and save m-files
I m-files: the files that save scripts and functions that you’ve
defined/created
I Workspace:
I Store all the variables that you currently created and defined
Help File
Command Prompt
I Open Hello_World.m
I % Name: Hello_World.m: % comments identifier
I %%: split m-file into subsections
I End a command with a semi-colon (;) to suppress the output
I Semi-colon (;) helps separate multiple commands that in one line
I Execute your commands
I Type (paste) commands in the command window → enter
I Define a 100 by 1 vector of ones
I Type ones(100,1) vs ones(100,1);
I Execution by selection: F9
I Execution by blocks: F5
Variables
I A variable stores the assigned value in memory so that your programs can read
it, operate on it and save it back to memory.
I Variable names:
Create Variables
1 1 2
I Create A = 3 B = [1 2] C = D= E = [0 0 0]
2 3 4
I A = 3
I []: Used to create matrix, not used to clarify order of operations.
I Row vector with two elements, separate the elements with either a comma (,) or
a space
B = [1,2] %or B = [1 2]
I A matrix has multiple rows, separate the rows with semicolons (;)
C = [1;2]
D = [1,2;3,4] %or D = [1 2; 3 4]
I Functions:
ones, zeros, rand, eyes
E = zeros(1,3)
I To reference a particular element in a matrix, specify its row and column number
I Always specify the row first and column second: A(row, column);
I A(2,3) refers to the 2nd row, 3rd column element.
I Colon (:) selects the complete rows or columns:; e.g. A(2, :) refers to the 2nd
row, whereas A(:, 3) refers to the 3rd column of matrix A.
I Colon (:) also selects a particular range of values in a matrix; e.g. A(2,1:3)
selects the first, second and third elements of the 2nd row.
I A(1 : 2, 3 : 4) selects the 2-by-2 sub-matrix of A that is comprised of the first 2
rows for the 3rd and 4th columns of A.
I a step size can also be used; e.g. A(1, 1 : 2 : 5) will select the first rows 1st, 3rd
and 5th elements
" #
1 2 3 4 5
I A=
6 7 8 9 10
I Find A(2, 3), A(2, 1 : 3), A(1 : 2, 3 : 4) and A(1, 1 : 2 : 5)
" #
1 2 3 4 5
A=
6 7 8 9 10
I A(2, 3) = 8
I A(2, 1 : 3) = 6, 7, 8
" #
3 4
I A(1 : 2, 3 : 4) =
8 9
I A(1, 1 : 2 : 5) = [1, 3, 5]
A+B Addition
A−B Subtraction
A∗B Multiplication
A0 Transpose
() evaluation order
element-by-element operations
4
−3 2 3
a = [1 2 3 4] b=
2 c=
1 4
4
I a ∗ b = 1 ∗ 4 + 2 ∗ (−3) + 3 ∗ 2 + 4 ∗ 4 = 20
(1 × 4) (4 × 1) (1 × 1)
7 18
I c2 =
6 19
2
32
2 4 9
I c.2 = =
12 42 1 16
Exercises
4
−3 2 3
I (1) Create a = [1 2 3 4] b= c= .
2 1 4
4
I (2) Calculate and create
I X1 = a ∗ b
I X2 = b ∗ a
I X3 = X 1 ∗ X 2
I X4 = a/2
I Extend Matrix X3 with a 5th column whose value is b, and call the
new Matrix M (eg. M = [X3,b])
I Try a ∗ b 0 (What the message says, why?)
I (3) perform element by element calculation a. ∗ b 0 , a0 . ∗ b, a0 ./b and c.2
I (4) Using the colon operator, create a 1-by-25 raw vector with:
Predefined Functions
Exercises 2
I (6) Using the functions zeros(.), ones(.) and eye(.) to create
I a 4-by-4 matrix A full of zeros
I a 3-by-4 matrix B full of ones
I a 5-by-5 identity matrix I
I a 4-by-4 matrix C full of fives
I
1 5 5 5
5 1 5 5
V =
5 5 1 5
5 5 5 1
I (7) Find the size and length of matrix B above
Examples