Download presentation
Presentation is loading. Please wait.
1
응용 전산 및 실습 MATLAB – Chapter 3 행렬연산
위덕대학교 에너지전기공학부 이 성 환 Chapter 3 Uiduk University
2
MATLAB – Scripting M-File
REVIEW MATLAB 메뉴의 File New M-File 선택 M-File Editor 가 실행되며 다음과 같이 화면에 나타남 원하는 MATLAB 명령어 입력 (명령어 입력 시 절대 한글을 사용하지 말 것!) File Save 를 선택하고 원하는 경로에 원하는 파일 이름으로 저장 (파일 이름에 절대 한글을 쓰지 말 것!) Debug Run 을 선택하여 생성한 M-File을 실행하면 MATLAB Command Window에 결과가 나타남 Chapter 3 Uiduk University
3
MATLAB – Enhancing M-File
REVIEW Keyword / Function Meaning Example ; Suppress output radius = 5; % Comment radius = 5; % 반지름 값 disp(variable) Display results without identifying variable names disp(radius) echo Control the ‘Command Window’ echoing of script file commands 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, var1, var2,,,) Print msg fprintf(‘계산이 완료되었습니다.’); fprintf(‘반지름은 %f 입니다.\n’, radius); waitforbuttonpress Pause until user presses mouse button or keyboard key Chapter 3 Uiduk University
4
MATLAB – Array Definition MATLAB – Array Calculation
REVIEW Keyword Meaning [ ] 배열의 시작과 끝 ; 행 구분 , 열 구분 MATLAB – Array Calculation 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, aT ‘ A’ Chapter 3 Uiduk University
5
MATLAB – Solving Linear Equations
REVIEW EX6) Solving the following linear equations >> A = [1, -1, 2; 3, 2, 9; 0, 1, -4] A = >> B = [10; 9; 3] B = 10 9 3 >> x = inv(A) * B x = 6.4783 Chapter 3 Uiduk University
6
MATLAB – Scalar and Array Operation
REVIEW Element-By-Element Operation Representative Data Scalar Addition Scalar Multiplication Array Addition Array Multiplication Array Division Array Exponentiation Chapter 3 Uiduk University
7
MATLAB – Standard Arrays
REVIEW 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 Chapter 3 Uiduk University
8
MATLAB – Easy Array Construction
Array Construction Technique Description x = [ 2, 2*pi, sqrt(5), 2-3j ] 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 10first, ending at 10last, having n elements Chapter 3 Uiduk University
9
MATLAB – Easy Array Construction
EX1) >> c = [1, 6, 9, 7] c = >> a = 1 : 5 a = >> b = 1 : 2 : 7 b = >> x = (0 : 0.1 : 0.3) * pi x = EX2) >> d = [ 1:2:5, 1, 0, 1 ] d = >> a = 1 : 3, b = 1 : 2 : 7 a = b = >> c = [ b, a ] c = EX3) >> x = linspace(2, 6, 3) x = >> x = logspace(0, 2, 11) Chapter 3 Uiduk University
10
MATLAB – Complex Array’s Transpose Pitfall
>> d = a + i * a d = i i i >> e = d’ % Complex conjugate transpose of d e = i i i >> f = d.’ % Dot transpose = Transpose of d i i i Chapter 3 Uiduk University
11
MATLAB – 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 Chapter 2 Uiduk University
12
MATLAB – Array Addressing
EX5) >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = >> A(3, 3) = 0 >> A(2, 6) = 1 % extends array A EX6) >> A( : , 4) = 4 A = >> B = A(1 : 2, 2 : 3) B = >> A( :, 2) = [ ] % removes 2nd column Chapter 3 Uiduk University
13
MATLAB – Array Addressing
EX7) >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = >> B = [1, 4, 7] B = >> A(2, : ) = B % substitutes 2nd row Chapter 3 Uiduk University
14
MATLAB – Array Manipulation
Function Description rot90(A) rot90(A, k) Rotates vector A by 90 degrees 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 : Chapter 3 Uiduk University
15
MATLAB – Array Manipulation
EX8) >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = >> flipud(A) ans = >> fliplr(A) EX9) >> rot90(A) ans = >> diag(A) 1 5 9 >> diag(ans) EX10) >> triu(A) ans = >> A = [1, 2, 3]; >> B = [4, 5, 6]; >> dot(A, B) 32 >> cross(A, B) Chapter 3 Uiduk University
16
MATLAB – Reading From Data File
<< mat.txt>> 파일의 내용 >> load mat.txt >> t = mat( : , 1); % 시간 벡터 >> x = mat( : , 2); % 변위 벡터 >> v = mat( : , 3); % 속도 벡터 >> a = mat( : , 4); % 가속도 벡터 Chapter 3 Uiduk University
17
MATLAB – Multidimensional Arrays
>> A = zeros(2, 3) % start with a 2-D array A = >> A( : , : , 2) = ones(2, 3) % add a second page to go 3-D ! A( : , : , 1) = A( : , : , 2) = Chapter 3 Uiduk University
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.