% ECE 1250 Lecture 4 Draw lines showing connecting the operator to the description: pp. 2-12 to 2-13 .* Addition (+) ^ Subtraction (-) / Multiplication (*) ' Division (/) \ Left division (\) + Power (^) * Complex conjugate transpose (') () Element-by-element multiply (.*) .' Element-by-element division (./) ./ Transpose (.') - Specify order of evaluation ( () ) %------------------------------------------------------------------------------- % Eye function. Matlab¨ Primer pp. 3-9 %------------------------------------------------------------------------------- >> Ident_mat = eye(3) % Create identity matrix. Ident_mat = 1 0 0 0 1 0 0 0 1 % Functions like eye, rand, ones, etc. allow for creation of a square matrix % when only one argument is used, or a specified number of rows and cols if % two arguments are used. >> eye(2,3) ans = 1 0 0 0 1 0 %------------------------------------------------------------------------------- % Functions with multiple return values, such as min and max p. 1-17 and size. %------------------------------------------------------------------------------- >> A = magic(3) A = 8 1 6 3 5 7 4 9 2 >> [max_val, index_of_max] = max(A) % finds max of cols max_val = 8 9 7 index_of_max = 1 3 2 % We choose the names of the returned variables such as max_val and index_of_max %------------------------------------------------------------------------------- % Size command p. 1-11, 2-29, length command p. 2-36. %------------------------------------------------------------------------------- >> B = [1, 2] B = 1 2 % The size function allows us to find out how big B is. >> [n_rows, n_cols] = size(B) nrows = 1 ncols = 2 % The size command may be used with two arguments to find out just one % dimension at a time. nrows = size(B,1) % The 1 means dimension 1, which is rows ncols = size(B,2) % The 2 means dimension 2, which is cols % The length command tells us the largest dimension of an array. >> B = ones(2,5); >> length(B) ans = 5 >> B = ones(4,3); >> length(B) ans = 4 % Length is especially useful for vectors (arrays with one row or col), % since you don't have to know whether the vector is horizontal or vertical. %------------------------------------------------------------------------------- % character strings pp. 1-15 to 1-16 %------------------------------------------------------------------------------- % We can create character variables. a = 'Hi' % Need an apostrophe inside the string? Use two apostrophes in a row. b = 'Let''s do this!' % How does Matlab know we are not done with the string % after the first ' ? Answer: it sees another ' . % If there is an odd number of ' you are in the string. >> n = str2num('15') % Convert a string into a number. n = 15 >> n = num2str(15) % Convert number into string. n = 15 % We can concatenate strings. Strings are just arrays. long_str = ['There are ', num2str(n), ' students in the lab'] % Be careful to put spaces in the string. % A simple command for displaying strings: >> display('long_str') % If you want an apostrophe in a string, use two apostrophes. >> display('That''s a magic square!') That's a magic square! %------------------------------------------------------------------------------- % Advanced Indexing. Continue from previous point in handout. % (See handout on website.) %-------------------------------------------------------------------------------