MATLAB Enhancing MFile Keyword Function Meaning Example Suppress
MATLAB – Enhancing M-File Keyword / Function Meaning Example ; Suppress output radius = 5; % Comment disp(variable) Display results without identifying variable names disp(radius) echo Control the ‘Command Window’ echoing of script file commands echo input Prompt user for input radius = input(‘반지름을 입력하세요> ‘); pause Pause until user presses any key pause(n) Pause for n seconds, then continue pause(3) fprintf(format, var 1, var 2, , , ) Print msg fprintf(‘계산이 완료되었습니다. ’); fprintf(‘반지름은 %f 입니다. n’, radius); waitforbuttonpress Pause until user presses mouse button or keyboard key waitforbuttonpress Chapter 3 radius = 5; % 반지름 값 Uiduk University
MATLAB – Array Definition Keyword Meaning [] 배열의 시작과 끝 ; 행 구분 , 열 구분 MATLAB – Array Calculation Chapter 3 Operation Symbol Example Addition, a + b + A+B Subtraction, a – b - A–B Multiplication, a • b * A*B Division, a b / or inv() A / B = A * inv(B) Exponentiation, ab ^ A^2 Transposition, a. T ‘ A’ Uiduk University
MATLAB – Solving Linear Equations EX 6) Solving the following linear equations >> A = [1, -1, 2; 3, 2, 9; 0, 1, -4] A= 1 -1 2 3 2 9 0 1 -4 >> B = [10; 9; 3] B= 10 9 3 >> x = inv(A) * B x= 6. 4783 -4. 0435 -0. 2609 Chapter 3 Uiduk University
MATLAB – Scalar and Array Operation Element-By-Element Operation Representative Data Scalar Addition Scalar Multiplication Array Addition Array Multiplication Array Division Array Exponentiation Chapter 3 Uiduk University
MATLAB – Standard Arrays Chapter 3 Keyword Description ones(n) ones(m, n) Create n x n arrays containing all ones Create m x n arrays containing all ones zeros(n) zeros(m, n) Create n x n arrays containing all zeros Create m x n arrays containing all zeros eye(n, n) eye(m, n) Produce n x n identity matrices Produce m x n identity matrices rand(n) rand(m, n) Uniformly distributed n x n random arrays Uniformly distributed m x n random arrays randn(n) randn(m, n) Zero-mean, unit-variance normal distribution n x n, m x n random arrays size(A) Return row & column size Uiduk University
MATLAB – Easy Array Construction Technique Description x = [ 2, 2*pi, sqrt(5), 2 -3 j ] Create row vector x containing element specified x = first : last Create row vector x starting with first, counting by one, ending at or before last x = first : increment : last Create row vector x starting with first, counting by increment, ending at or before last x = linspace(first, last, n) Create row vector x starting with first, ending at last, having n elements x = logspace(first, last, n) Create logarithmically-spaced row vector x starting with 10 first, ending at 10 last, having n elements Chapter 3 Uiduk University
MATLAB – Easy Array Construction EX 1) EX 2) EX 3) >> c = [1, 6, 9, 7] >> d = [ 1: 2: 5, 1, 0, 1 ] >> x = linspace(2, 6, 3) c= d= x= 1 6 9 7 1 3 5 1 0 2 1 4 6 >> a = 1 : 5 >> a = 1 : 3, b = 1 : 2 : 7 >> x = logspace(0, 2, 11) a= a= x= 1 2 3 4 5 1 2 3 3 5 1. 0000 1. 5849 2. 5119 3. 9811 6. 3096 10. 0000 15. 8489 25. 1189 39. 8107 63. 0957 100. 0000 b= >> b = 1 : 2 : 7 1 7 b= 1 3 5 7 >> c = [ b, a ] c= >> x = (0 : 0. 1 : 0. 3) * pi 1 3 5 7 1 2 3 x= 0 Chapter 3 0. 3142 0. 6238 0. 9425 Uiduk University
MATLAB – Complex Array’s Transpose Pitfall EX 4) >> a = 1 : 3 a= 1 2 3 >> d = a + i * a d= 1. 0000 + 1. 0000 i >> e = d’ 2. 0000 + 2. 0000 i 3. 0000 + 3. 0000 i % Complex conjugate transpose of d e= 1. 0000 - 1. 0000 i 2. 0000 - 2. 0000 i 3. 0000 - 3. 0000 i >> f = d. ’ % Dot transpose = Transpose of d 1. 0000 + 1. 0000 i 2. 0000 + 2. 0000 i 3. 0000 + 3. 0000 i Chapter 3 Uiduk University
MATLAB – Array Addressing Chapter 2 Array Addressing Description A(r, c) Indicates the elements by the r’th row and the c’th column A(r, : ) Addresses a subarray within A defined by the index vector of desired rows in r and all columns A( : , c) Addresses a subarray within A defined by all rows and the index vector of desired columns in c A( a : b , c : d) Addresses a subarray within A intersected by the rows from a to b and the columns from c to d Uiduk University
MATLAB – Array Addressing EX 5) EX 6) >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] >> A( : , 4) = 4 A= A= 1 2 3 4 0 0 4 5 6 4 0 1 7 8 9 7 8 0 4 0 0 >> A(3, 3) = 0 >> B = A(1 : 2, 2 : 3) A= B= 1 2 3 4 5 6 7 8 0 >> A( : , 2) = [ ] >> A(2, 6) = 1 % extends array A A= % removes 2 nd column A= 1 3 4 0 0 1 2 3 0 0 0 4 6 4 0 1 4 5 6 0 0 1 7 0 4 0 0 7 8 0 0 Chapter 3 Uiduk University
MATLAB – Array Addressing EX 7) >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A= 1 2 3 4 5 6 7 8 9 >> B = [1, 4, 7] B= 1 4 7 >> A(2, : ) = B % substitutes 2 nd row A= Chapter 3 1 2 3 1 4 7 7 8 9 Uiduk University
MATLAB – Array Manipulation Chapter 3 Function Description rot 90(A) rot 90(A, k) Rotates vector A by 90 degrees * k (k > 0 = ccw, k < 0 = cw) flipud(A) Flips array in up-down direction fliplr(A) Flips array in the left-right direction triu(A) Extracts upper triangular part tril(A) Extracts lower triangular part diag(A) Extracts diagonal elements cross(A, B) Outer product of A and B : dot(A, B) = sum(A. *B) Inner product of A and B : Uiduk University
MATLAB – Array Manipulation EX 8) EX 9) EX 10) >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] >> rot 90(A) >> triu(A) A= ans = 1 2 3 3 6 9 1 2 3 4 5 6 2 5 8 0 5 6 7 8 9 1 4 7 0 0 9 >> flipud(A) >> diag(A) >> A = [1, 2, 3]; ans = >> B = [4, 5, 6]; 7 8 9 1 >> dot(A, B) 4 5 6 5 ans = 1 2 3 9 32 >> fliplr(A) >> diag(ans) >> cross(A, B) ans = Chapter 3 3 2 1 1 0 0 6 5 4 0 5 0 9 8 7 0 0 9 -3 6 -3 Uiduk University
MATLAB – Reading From Data File << mat. txt>> 파일의 내용 0. 7918 1. 2590 -0. 2172 -0. 9242 2. 6448 0. 1716 -0. 5228 0. 3639 4. 5700 -0. 1982 0. 0586 0. 1214 6. 4240 -0. 0158 0. 0763 -0. 0615 8. 3750 0. 0303 -0. 0131 -0. 0144 10. 2545 0. 0003 -0. 0104 0. 0101 12. 2296 -0. 0044 0. 0026 0. 0014 14. 1076 0. 0003 0. 0013 -0. 0016 16. 0704 0. 0006 -0. 0005 -0. 0001 18. 2613 -0. 0001 0. 0002 >> load mat. txt Chapter 3 >> t = mat( : , 1); % 시간 벡터 >> x = mat( : , 2); % 변위 벡터 >> v = mat( : , 3); % 속도 벡터 >> a = mat( : , 4); % 가속도 벡터 Uiduk University
MATLAB – Multidimensional Arrays >> A = zeros(2, 3) % start with a 2 -D array A= 0 0 0 >> A( : , 2) = ones(2, 3) % add a second page to go 3 -D ! A( : , 1) = 0 0 0 A( : , 2) = Chapter 3 1 1 1 Uiduk University
- Slides: 17