Download presentation
Presentation is loading. Please wait.
Published byTheodora Welch Modified over 9 years ago
1
September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB http://www.etcs.ipfw.edu/~lin
2
September 15, 2005 Lecture 5 - By Paul Lin 2 Lecture 5: More On MATLAB Arrays 5.1 More MATLAB Functions 5.2 Array Operators 5.3 Array Arithmetic Operations 5.4 Sub-arrays
3
September 15, 2005 Lecture 5 - By Paul Lin 3 5.1 More MATLAB Functions size(this_array) returns the two values specifying the number of rows and columns in this_arrayreturns the two values specifying the number of rows and columns in this_arraylength(this_array) returns the length of a vector, or the longest dimension of a 2-D arrayreturns the length of a vector, or the longest dimension of a 2-D arrayzeros(n) built-in MATLAB function for creating an n- by-n array with all elements are initialized to zerobuilt-in MATLAB function for creating an n- by-n array with all elements are initialized to zero zeros(n, m) for creating an n-by-m all-zero arrayfor creating an n-by-m all-zero array
4
September 15, 2005 Lecture 5 - By Paul Lin 4 5.1 More MATLAB Functions (continue) ones(n) built-in MATLAB function for creating an n- by-n array with all elements are initialized to onebuilt-in MATLAB function for creating an n- by-n array with all elements are initialized to one ones(n, m) for creating an n-by-m all-one arrayfor creating an n-by-m all-one arrayeye(n) for creating an n-by-n identity matrix in which all elements in the main diagonal are onesfor creating an n-by-n identity matrix in which all elements in the main diagonal are ones eye(n, m) for creating an n-by-m identity matrixfor creating an n-by-m identity matrix
5
September 15, 2005 Lecture 5 - By Paul Lin 5 5.1 More MATLAB Functions (continue) >> help rand RAND Uniformly distributed random numbers. RAND(N) is an N-by-N matrix with random entries, chosen from a uniform distribution on the interval (0.0,1.0). RAND(N) is an N-by-N matrix with random entries, chosen from a uniform distribution on the interval (0.0,1.0). RAND(M,N) and RAND([M,N]) are M-by-N matrices with random entries. RAND(M,N,P,...) or RAND([M,N,P,...]) generate random arrays. RAND with no arguments is a scalar whose value changes each time it is referenced. RAND(SIZE(A)) is the same size as A.
6
September 15, 2005 Lecture 5 - By Paul Lin 6 5.1 More MATLAB Functions (continue) >> help fix FIX Round towards zero. FIX Round towards zero. FIX(X) rounds the elements of X to the nearest integers towards zero. See also FLOOR, ROUND, CEIL. See also FLOOR, ROUND, CEIL.
7
September 15, 2005 Lecture 5 - By Paul Lin 7 5.1 More MATLAB Functions (continue) Example 5.1: Creating arrays using array functions n = 3; m = 2; % an 3 x 3 all-zero array A0 = zeros(3) % an 3 x 2 all zero array B0 = zeros(3, 2) % an 3 x 3 all-one array A1 = ones(3) % an 3 x 2 all-zero array B1 = ones(3,2) %An identify array A_eye = eye(n) A1 = 1 1 1 B1 = 1 1 A_eye = 1 0 0 0 1 0 0 0 1 A0 = 0 0 0 B0 = 0 0
8
September 15, 2005 Lecture 5 - By Paul Lin 8 5.2 Array Operators Transpose operator ( ' ) swap the rows and columns of an array swap the rows and columns of an array Colon operator (: ) one of the most useful operator in MATLAB. We can use it to create regularly spaced vectors, subscript matrices, and specify for iterationsone of the most useful operator in MATLAB. We can use it to create regularly spaced vectors, subscript matrices, and specify for iterations
9
September 15, 2005 Lecture 5 - By Paul Lin 9 5.2 Array Operators Example 5.2: Vector Transpose Example rand() – random number generation function rand() – random number generation function >> % Generate an array using rand() function array_r1 = rand(1, 4) array_r2 = rand(1, 4) % Convert row vector to column vector array_c1 = array_r1' array_c2 = array_r2' array_r1 = 0.2722 0.1988 0.0153 0.7468 array_r2 = 0.4451 0.9318 0.4660 0.4186 array_c1 = 0.2722 0.2722 0.1988 0.1988 0.0153 0.0153 0.7468 0.7468 array_c2 = 0.4451 0.4451 0.9318 0.9318 0.4660 0.4660 0.4186 0.4186
10
September 15, 2005 Lecture 5 - By Paul Lin 10 5.2 Array Operators (continue) Example 5.3: Using rand(), fix() functions and array transpose operator >> A = rand(1,4) *10 A = 0.5789 3.5287 8.1317 0.0986 0.5789 3.5287 8.1317 0.0986 >> A = fix(A) A = 0 3 8 0 0 3 8 0 >> B = fix(rand(1,4)*10) B = 1 2 1 6 1 2 1 6 >> C = [A' B'] C = 0 1 0 1 3 2 3 2 8 1 8 1 0 6 0 6
11
September 15, 2005 Lecture 5 - By Paul Lin 11 5.2 Array Operators (continue) Example 5.4: Using colon operator to pick-up selected rows or columns Example 5.4: Using colon operator to pick-up selected rows or columns A(:, j) – extracts j-th column of A A(:, j) – extracts j-th column of A A(i, :) – extracts the i-th row of A A(i, :) – extracts the i-th row of A C = 0 1 0 1 3 2 3 2 8 1 8 1 0 6 0 6 C_1 = C(:,1) C_2 = C(:, 2) R_1 = C(1,:) R_2 = C(2, :) R_3 = C(3, :) R_4 = C(4, :) R_1 = 0 1 0 1 R_2 = 3 2 3 2 R_3 = 8 1 8 1 R_4 = 0 6 0 6 C_1 = 0 3 8 0 C_2 = 1 2 1 6
12
September 15, 2005 Lecture 5 - By Paul Lin 12 5.2 Array Operators (continue) Example 5.5: This problem solving example uses the colon operator with integers to generate a regularly spaced temperature vector from 0 to 100 in degree C. We will also print all data to show that C to F Temperature Conversion holding a linear relationship. Example 5.5: This problem solving example uses the colon operator with integers to generate a regularly spaced temperature vector from 0 to 100 in degree C. We will also print all data to show that C to F Temperature Conversion holding a linear relationship. Analysis (identified equations and MATLAB equations, and using plot function to show the linear relationship) Analysis (identified equations and MATLAB equations, and using plot function to show the linear relationship) F = 9/5 C + 32 C = 0:10:100; F = 9/5 * C + 32; plot(C, F)
13
September 15, 2005 Lecture 5 - By Paul Lin 13 5.2 Array Operators (continue) Example 5.5: Solution Example 5.5: Solution % CtoF_plot.m % Author: M. Lin % Date: 9/6/04 % Description: C = 0:10:100; F = (9/5)* C + 32; plot(C, F), grid on xlabel('Degree C'), ylabel('Degree F') title(' C vs F')
14
September 15, 2005 Lecture 5 - By Paul Lin 14 5.2 Array Operators (continue) Example 5.6: Reconstruct the sine 60Hz signal and its time vector as a 2-D array and save it as sine60hz.mat file. Verify result by reloading the program and plot the sine wave using colon operator. Example 5.6: Reconstruct the sine 60Hz signal and its time vector as a 2-D array and save it as sine60hz.mat file. Verify result by reloading the program and plot the sine wave using colon operator. Solution: Solution: f = 60 Hz, T = 1/f f = 60 Hz, T = 1/f Vm = 10 volts, Vm = 10 volts, dt = 0.001*T, dt = 0.001*T, t = 0:dt: 5*T t = 0:dt: 5*T e = Vm*sin(2*pi*f*t) e = Vm*sin(2*pi*f*t) % sine60hz_2d.m % Author: M. Lin % Date: 9/6/04 % Description: f = 60; T = 1/f; Vm = 10; dt = 0.001*T; t = 0:dt: 5*T; e = Vm*sin(2*pi*f*t); sine60 = [t' e']; save sine60.mat sine60 clear load sine60.mat plot(sine60(:,1), sine60(:,2))
15
September 15, 2005 Lecture 5 - By Paul Lin 15 5.2 Array Operators (continue) Example 5.6: MATLAB Solution Example 5.6: MATLAB Solution
16
September 15, 2005 Lecture 5 - By Paul Lin 16 5.3 Array Arithmetic Operations + Addition A + B adds A and B arrays of the same dimension, unless one is a scalar. A scalar can be added to a matrix of any dimension.A + B adds A and B arrays of the same dimension, unless one is a scalar. A scalar can be added to a matrix of any dimension. An Example: both A and B are 2-by-2 array, A + B isAn Example: both A and B are 2-by-2 array, A + B is | (a11 + b11) (a12 + b12) | | (a21 + b21) (a22 + b22) |
17
September 15, 2005 Lecture 5 - By Paul Lin 17 5.3 Array Arithmetic Operations (continue) - Subtraction. - Subtraction. A - B subtracts B from A. A and B arrays must have the same dimension, unless one is a scalar. A scalar can be subtracted from a matrix of any dimension.A - B subtracts B from A. A and B arrays must have the same dimension, unless one is a scalar. A scalar can be subtracted from a matrix of any dimension. An Example: both A and B are 2-by-2 array, A - B isAn Example: both A and B are 2-by-2 array, A - B is | (a11 - b11) (a12 - b12) | | (a21 - b21) (a22 - b22) |
18
September 15, 2005 Lecture 5 - By Paul Lin 18 5.3 Array Arithmetic Operations (continue).* Array Multiplication A.* B is the element-by-element product of the arrays A and B. A and B must have the same dimension, unless one is a scalar. A scalar can be multiplied to a matrix of any dimension.A.* B is the element-by-element product of the arrays A and B. A and B must have the same dimension, unless one is a scalar. A scalar can be multiplied to a matrix of any dimension. An Example: both A and B are 2-by-2 array, A.* B isAn Example: both A and B are 2-by-2 array, A.* B is | (a11 * b11) (a12 * b12) | | (a21 * b21) (a22 * b22) |
19
September 15, 2005 Lecture 5 - By Paul Lin 19 5.3 Array Arithmetic Operations (continue)./ Array Right Division A./B is the element-by-element division of the arrays A and B. A and B must have the same dimension, unless one is a scalar. A scalar can be divided by a matrix of any dimension.A./B is the element-by-element division of the arrays A and B. A and B must have the same dimension, unless one is a scalar. A scalar can be divided by a matrix of any dimension. An Example: both A and B are 2-by-2 array, A./ B isAn Example: both A and B are 2-by-2 array, A./ B is | (a11 / b11) (a12 / b12) | | (a21 / b21) (a22 / b22) |
20
September 15, 2005 Lecture 5 - By Paul Lin 20 5.3 Array Arithmetic Operations (continue) Example 5.7: Element-by- element array arithmetic operations A = fix(rand(2)*10) B = fix(rand(2)*10) W = A - B X = A + B Y = A.* B Z = A./ B >> A = fix(rand(2)*10) B = fix(rand(2)*10) W = A - B X = A + B Y = A.* B Z = A./ B A = 9 6 2 4 B = 8 4 7 0
21
September 15, 2005 Lecture 5 - By Paul Lin 21 5.3 Array Arithmetic Operations (continue) Example 5.7: continue W = A - B X = A + B Y = A.* B Z = A./ B A = 9 6 9 6 2 4 2 4 B = 8 4 8 4 7 0 7 0 >> A - B W = 1 2 1 2 -5 4 -5 4 >> A + B X = 17 10 17 10 9 4 9 4 >> A.* B Y = 72 24 72 24 14 0 14 0 >> A./B Warning: Divide by zero. (Type "warning off MATLAB: divideByZero" to suppress this warning.) Z = 1.1250 1.5000 1.1250 1.5000 0.2857 Inf 0.2857 Inf
22
September 15, 2005 Lecture 5 - By Paul Lin 22 5.4 SubArrays Subarrays can be formed by using colon (: ) operator to select a portion of an array Example 5.9: Subarray Examples array4 = [10 20 30; -20 -30 -40; 30 40 50] array5 = array4(1, : ) % [10 20 30] array6 = array4(:,1: 2: 3) % [first column third column] array6 = 10 30 10 30 -20 -40 -20 -40 30 50 30 50 array1 = [1 2 3 4 5]; array7 = array1(3: end) array 7 = 3 4 5 array1(end) ans = 5
23
September 15, 2005 Lecture 5 - By Paul Lin 23 Summary 5.1 More MATLAB Functions 5.2 Array Operators 5.3 Array Arithmetic Operations 5.4 Subarrays
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.