% ECE 1250 Lecture 2 %------------------------------------------------------------------------------- % Variables Primer pp. 1-4 %------------------------------------------------------------------------------- % Matlab works like a calculator but with answers stored in variables: >> a2 = log(3) % We give the answer a name "a2" (we choose the name) a2 = 1.0986 >> d = a2 + 3 % We can refer to a previous answer by its name d = 4.0986 % Variable names start with a letter. After that, we may use letters, numbers, % and underscores, but no spaces inside the variable name. % Capitalization matters in array names. A2 is Not the same as a2. %------------------------------------------------------------------------------- % Arrays Primer pp. 1-6 %------------------------------------------------------------------------------- % Matlab works like a calculator but with arrays: pp. 1-6 to 1-9 >> Z = zeros(2,3) % Creates a matrix with 2 rows and 3 cols of 0's. Z = 0 0 0 0 0 0 % Matlab does not print brackets around the arrays (that is, matrices) that it % prints out. Nor does it print commas between values. >> a = [1, 2, 3, 5] % This creates a vector that is horizontal. % Note the square brackets used for defining arrays. % Extra white space between characters in command is okay. a = 1 2 3 5 >> sin(a) % Wow! We get the sines of all the values in "a". ans = 0.8415 0.9093 0.1411 -0.9589 % If you omit the variable being set equal to your expression, Matlab calls it % "ans". The name "ans" always refers to the result of the previous command. % You may use the name "ans" in a command (as you would on a TI calculator). %------------------------------------------------------------------------------- % Matrices Primer pp. 1-7 to 1-9 %------------------------------------------------------------------------------- >> B = [1, 2; 3, 5] % This creates a matrix. The ; starts a new row. ans = 1 2 3 5 >> cos(B) + 4 % cosines plus 4 for every value in matrix B. ans = 4.5403 3.5839 3.0100 4.2837 % NOTE: Matlab¨ usally insists that sizes of arrays be exactly the same when you % add them, but an exception is made if you add a scalar to an array. % Beware the inconsistencies of Matlab¨. >> x = [0; 1] % A vertical vector. Be careful about horizontal versus % vertical. x = 0 1 >> x' % Apostrophe is transpose. It makes horizontal vertical % and vice versa. ans = 0 1 >> B' % Transpose reflects around diagonal. ans = 1 3 2 5 >> y = B * x % * is scalar Or matrix multiplication. % Remember that matrix multiplication B*x goes across the rows of B and down the % columns of x. y = 2 5 >> y = inv(B) * x % It's easy to invert a matrix. y = 2 -1 >> Bsq = B * B % Matrix B times matrix B. Bsq = 7 12 18 31 >> Bsq = B ^ 2 % Same thing as B*B. Bsq = 7 12 18 31 % What if we want to multiply B by B element by element? >> BB = B .* B % A new .* operator that means "element-by-element". Bsq = 1 4 9 25 >> BB = B .^ 2 % Same thing as B.*B. Bsq = 1 4 9 25 >> One = B ./ B % Divide entries in B by respective entry in B. % Result is all ones, unless entry in B equals zero. Bsq = 1 1 1 1 % We can build bigger arrays from smaller arrays using concatenation: >> bigB = [B, B, B] % Three copies of B side-by-side. An array of arrays. bigB = 1 2 1 2 1 2 3 5 3 5 3 5 >> C = [B; [3,4]] % Add a row on the bottom of B. C = 1 2 3 5 3 4 % If you use the wrong shape of array, Matlab will complain! >> D = [B; 0] % Oops! We get cryptic error messages. Error using vertcat CAT arguments dimensions are not consistent. %------------------------------------------------------------------------------- % Concatenation Primer p. 1-9 %------------------------------------------------------------------------------- % This comes much later, but it is useful now: we can create an empty array (and then make it bigger). >> emp = [ ] % Like an empty set. This is valid. emp = [ ] >> emp = [emp, 2] % We can concatenate with an empty array. emp = 2 >> emp = [emp, 2] % We can concatenate again. emp = 2 2 %------------------------------------------------------------------------------- % Complex Numbers Primer p. 1-10 %------------------------------------------------------------------------------- % Matlab is comfortable with complex numbers. >> woah = sqrt(-B) % Note: i = sqrt(-1) is imaginary number. Can also use j. woah = 0 + 1i 0 + 1.414i 0 + 1.732i 0 + 2.361i >> f = 3 + 4i % We don't need to say 3 + 4 * i, although that would % also work. ans = 3 + 4i % j is the same as i for EE's % This takes us through pg 1-10 of the Matlab¨ Primer.