% ECE 1250 Lecture 3 %------------------------------------------------------------------------------- % Script Files % Matlab¨ Primer pp. 1-24 to 1-25. %------------------------------------------------------------------------------- % A script file is a plain-text file with a .m extension. Type commands into % the script file just as you would at the Matlab¨ prompt. To execute the % script file, type the filename (without the .m) at the >> command prompt. % Use the "Set Path" command under the File menu to add the folder containing % your script file to the Matlab¨ search path, or change your working directory % to the folder containing your script file. % The following lines are put into a script file called example.m t = [0, 1, 2, 3] sin(t) % To execute the above file, we type the name of the file at the command prompt % in Matlab¨. >> example t = 0 1 2 3 ans = 0 0.8415 0.9092 0.1411 %------------------------------------------------------------------------------- % Array Indexing and the colon operator. % Matlab¨ Primer pp. 1-11 to 1-12. %------------------------------------------------------------------------------- % The magic function creates magic squares where all rows and cols sum to % same number. >> A = magic(3) A = 8 1 6 3 5 7 4 9 2 % Basic Indexing. Matlab¨ Primer pp. 1-11 to 1-12 % To extract (or set) certain values, we use indexing by row and col numbers: >> A(3,2) % Extract the element in row 3 (bottom row) and 2nd col (middle col) ans = 9 % Note that we must use the curved ( ) parentheses for indexing. >> A(4,5) % There is no row 4 and no col 5, so this results in an error. ??? Index exceeds matrix dimensions. % A curious thing happens, however, if we try to set the value of A(4,5). >> A(4,5) = 10 8 1 6 0 0 3 5 7 0 0 4 9 2 0 0 0 0 0 0 10 % Matlab¨ makes the A matrix bigger, sets A(4,5) to 10 and sets other % added values to zero. % If we want the last entry in a row or column, we can say "end". The word % "end" is used for ending loops and if statements, too, so it's meaning % changes depending on where it is used. >> A(4, end) ans = 10 >> A(end, 5) ans = 10 %------------------------------------------------------------------------------- % Colon Operator. Matlab¨ Primer p. 1-12 %------------------------------------------------------------------------------- % Two different uses for : operator, depending on where it is used: % 1) Use : to select an entire row or column, or % 2) Use : to generate a sequence of evenly spaced values % Using colon operator to select an entire row or column. % Reset A to 3 x 3 magic square. >> A = magic(3) A = 8 1 6 3 5 7 4 9 2 % Select the entire 3rd row. : means "all possible index values for col #" >> A(3,:) ans = 4 9 2 % Select the entire 2nd column. >> A(:,2) ans = 1 5 9 % We also use the colon operator to generate a sequence (array) of numbers. >> 1:3 ans = 1 2 3 % We get an array, which is horizontal. % What if we want a vertical array? >> 1:3' % What happens first, (i.e., binds tighter), the transpose % or the sequence generation? ans = 1 2 3 % The transpose of just the 3 occurred first. Too bad. % Try again using ( ) to change order of operations. >> (1:3)' ans = 1 2 3 % Note that [ ] instead of ( ) would also work, but the [ ] would create an % array of an array, which would be an array, and the array creation would % happen before the transpose. % We can ue the colon operator to create a range of index values. >> A(1:3,2) % Extract rows 1 through 3 in column 2 of A ans = 1 5 9 >> A(2:end,2) % Extract rows 2 onward, col 2 ans = 5 9 % We can space a sequence by values other than 1. >> 3:-1:2 ans = 6 4 2 >> A(3:-1:2,2) % This gives the 2nd column upside down. ans = 9 5 1 % We can also use noninteger increments. >> 2: 0.1 : 2.35 ans = 2 2.1 2.2 2.3 >> A(2:0.1:2.35,2) % Indexes should be integers. Matlab¨ rounds down to 2. ans = 5 5 5 5 % Hmm. We got four values of A(2,2), but A only has 3 rows. What's going on? % Time for more advanced indexing info. %------------------------------------------------------------------------------- % Indexing using arrays of Row and Col values %------------------------------------------------------------------------------- % See "Advanced Indexing" handout on course website, section on using arrays % of row and col indices. (Not all of this is in "Matlab¨ Primer".) %------------------------------------------------------------------------------- % sum() function pp. 2-5 to 2-7 %------------------------------------------------------------------------------- % Reset A to 3 x 3 magic square. >> A = magic(3) A = 8 1 6 3 5 7 4 9 2 % Take the sums of the columns. Functions operating on 2-dimensional arrays % typically produce an answer for each column. >> sum(A) % Calculate the sums of cols 1, 2, and 3 ans = 15 15 15 % The column sums are all the same because A is a magic square. % If we apply sum() to a one-row array, (i.e., a vector), then Matlab¨ decides % to take the sum of the row. >> sum([15, 15, 15]) % Or we could have said sum(sum(A)) ans = 45 % Note: this is typical Matlab¨ behavior and is arguably inconsistent. Like % other features of Matlab¨, this "feature" was meant to streamline % programs. Unfortunately, it makes the behavior of Matlab¨ appear % arbitrary to the uninitiated. %------------------------------------------------------------------------------- % Other useful Functions operating on Columns. p. 1-17 %------------------------------------------------------------------------------- % min(), max(), mean() (averages values), sort() % A useful function for extracting the diagonal of a matrix. p. 2-5 % diag() %------------------------------------------------------------------------------- % Generating matrices. pp. 2-7 to 2-9 %------------------------------------------------------------------------------- % To create a matrix of zeros with, for example, 2 rows and 4 columns: >> Z = zeros(2,4) Z = 0 0 0 0 0 0 0 0 % To create a matrix of ones with, for example, 3 rows and 2 columns: >> One32 = ones(3,2) 1 1 1 1 1 1 % To create a matrix of fives with, for example, 1 row and 3 columns: >> Five13 = 5 * ones(1,3) Five13 = 5 5 5 % To create a matrix of random numbers between 0 and 1 with, for example % 2 rows and 3 columns: >> R = rand(2,3) R = 0.9501 0.6068 0.8913 0.2311 0.4860 0.7621 % To create a matrix of random numbers that are normally (or gaussian) % distributed with, for example 3 rows and 4 columns: >> Gaussian = randn(3,4) Gaussian = -0.4326 0.1253 -1.1465 -1.6656 0.2877 1.1909