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 2 radius = 5; % 반지름 값 Uiduk University
MATLAB – Array Definition Keyword Meaning [] 배열의 시작과 끝 ; 행 구분 , 열 구분 MATLAB – Array Calculation Chapter 2 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 – Array Calculation Chapter 2 EX 1) EX 2) EX 3) >> A = [1, 2; 3, 4] >> D = A - B >> E = A / B A= D= E= 1 2 -4 -4 3. 0000 -2. 0000 3 4 -4 -4 2. 0000 -1. 0000 >> B = [5, 6; 7, 8] >> H = A * B >> F = A * inv(B) B= H= F= 5 6 19 22 3. 0000 -2. 0000 7 8 43 50 2. 0000 -1. 0000 >> C = A + B >> G = A ^ 2 >> J = A - 3 C= G= J= 6 8 7 10 -2 -1 10 12 15 22 0 1 Uiduk University
MATLAB – Array Indexing & Transposition EX 4) EX 5) >> A = [1, 2; 3, 4] >> A(2, 1) A= ans = 1 2 3 4 3 >> A(2, 2) = -2 >> A(2, 2) A= ans = 1 2 3 -2 -2 >> A' ans = Chapter 2 1 3 2 -2 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 2 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 2 Uiduk University
MATLAB – Scalar and Array Operation EX 7) >> A = [2, 4; 5, 6] A= 2 4 5 6 >> B = [3, 4; 5, 6] B= 3 4 5 6 >> C = A. *B C= Chapter 2 6 16 25 36 Uiduk University
MATLAB – Standard Arrays Chapter 2 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 – Standard Arrays EX 8) EX 9) EX 10) >> ones(3) >> eye(2, 4) >> A = [2, 3, 1; -2, 8, 9]; ans = A= 1 1 0 0 0 2 3 1 1 0 1 0 0 -2 8 9 1 1 1 >> zeros(2, 5) ans = Chapter 2 >> rand(3) >> size(A) ans = 0. 9501 0. 4860 0. 4565 0 0 0. 2311 0. 8913 0. 0185 0 0 0. 6068 0. 7621 0. 8214 >> eye(2) >> randn(3) ans = 1 0 0. 8580 -1. 4410 0. 6900 0 1 1. 2540 0. 5711 0. 8156 -1. 5937 -0. 3999 0. 7119 2 3 Uiduk University
- Slides: 12