210 likes | 331 Vues
Announcements. our final exam is the last week of class, in your lab period Lab 10 will be our final and third-graded lab new grading policy: final lab grade = ( ( Lab3 + lab6 + lab10 + best lab )/4 ) * 30%. m files. type commands into MATLABs "notepad" load/save as usual
E N D
Announcements • our final exam is the last week of class, in your lab period • Lab 10 will be our final and third-graded lab • new grading policy: final lab grade = ( ( Lab3 + lab6 + lab10 + best lab)/4 ) * 30%
m files • type commands into MATLABs "notepad" • load/save as usual • two ways to run: • hit the PLAY button • Type the filename at the command prompt (without the .m)
rand • I = rand(1,3) % rand(m,n) gives m*n matrix of uniformly distributed random numbers from 0 - .9999 >> I = rand(1,3) I = 0.8147 0.9058 0.1270
>> A = [rand(1,3); rand(1,3)*10; rand(1,3)*100 ] A = 0.8235 0.6948 0.3171 9.5022 0.3445 4.3874 38.1558 76.5517 79.5200
Create a 3 by 3 matrix with each element a random value between 0 and 9 • rand(3, 3) create a 3 by 3 matrix with random values between 0 and 1 • rand(3,3)*10 create a 3 by 3 matrix with random values between 0 and 9
the single dimension z = rand( 10 ) gives a 10 x 10 matrix q = ones( 10 ) gives a 10 x 10 matrix
transposing - swap rows and columns A = [ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 ] >> B = transpose(A) B = [16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1 ]
transposing a row makes it a column >> x = [0:6] x = 0 1 2 3 4 5 6 >> y = transpose(x) y = 0 1 2 3 4 5 6 >>
transposing a column makes it a row y = 0 1 2 3 4 5 6 >> z = transpose(y) z = 0 1 2 3 4 5 6 >>
transposing a 2 x 5 matrix >> A = [ 5 7 9 0 12 ; 16 3 44 1 8 ] A = 5 7 9 0 12 16 3 44 1 8 >> B = transpose(A) B = 5 16 7 3 9 44 0 1 12 8 >> 2 x 5 5 x 2
Individual Matrix elements • Let's start with the simple case of a vector and a single subscript. The vector is v = [16 5 9 4 2 11 7 14] • The subscript can be a single value. v(3) % Extract the third element ans = 9 • Colons work: v(1:4) ans = 16 5 9 4
Now consider indexing into a matrix. A = [ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 ] • indexing in matrices is done using two subscripts - one for the rows and one for the columns. A(2,4) % Extract the element in row 2, column 4ans = 8
who and whos • who shows your variables • whos lists your variables and their characteristics clc and clear • clc clears the command window • clear erases all variables
.mat files and the SAVE command http://www.mathworks.com/help/matlab/ref/save.html saves all the variables and current values in a .mat file different than saving a script file more like saving your desktop, or workspace, not your commands
the SAVE command http://www.mathworks.com/help/matlab/ref/save.html saves all the variables and current values in a .mat file different than saving a script file more like saving your desktop, or workspace, not your commands
file extension .mat is automatic save myStuff % saves all variables to myStuff.mat load myStuff % loads variables from myVars.mat
e.g. surface plot • x=[1:10] • y=transpose(x) • %matrix mult: • z= y * x • figure(1) • surf(x,y,z) • figure(2) • z = rand(10) • surf(x,y,z)
matrix multiplication? • it's an odd operation, not just multiplying corresponding elements • matrices can be multiplied if their inside dimensions match: • (m x n) * (n x q) • e.g. a (5 x 4) CANNOT multiply a (2 x 3) • the resulting matrix has the outside dimensions: • (m x n) * (n x q) = (m x q) matrix
matrix multiplication simple A * B = C each Row in A x each Col in B = (Row,Col) item in C a11b12 + a12b22 = c12
1*1 + 2*3 + 3*5 = 22 row 1 column 1 item (1, 1) 1 2 3 * 1 2 = 22 28 4 5 6 3 4 49 64 5 6 2 X 3 * 3 X 2 results in a 2 x 2 inner dimensions must be the same out dimensions reveal size
Watch the video 5.4 • Harvey explains how to multiply matrices