190 likes | 383 Views
MATLAB. http://blog.naver.com/gajest. Contents. 1. Introduction. 2. Control Flow. 3. Functions. 4. Exercises. Introduction. Matlab 에 있는 흐름 제어 방법을 공부 세가지 IF 문 소개 (IF-END, IF-ELSE-END, IF-ELSEIF-ELSE-END) SWITCH-CASE 문 사용 및 세가지 IF 문과의 비교 FOR 루프 문 ( 단독 및 중첩 ) 문 사용
E N D
MATLAB http://blog.naver.com/gajest
Contents 1. Introduction 2. Control Flow 3. Functions 4. Exercises DSRL
Introduction • Matlab에 있는 흐름 제어 방법을 공부 • 세가지 IF 문 소개 (IF-END, IF-ELSE-END, IF-ELSEIF-ELSE-END) • SWITCH-CASE문 사용 및 세가지 IF문과의 비교 • FOR 루프 문(단독 및 중첩) 문 사용 • WHILE루프 문 사용 및 FOR루프와의 비교 • Functions의 사용 DSRL
Control Flow • IF-END • 기본 사용법 if 조건 명령어 문장 end if 문장의 조건(식)이 참(1)이면 명령어 수행 거짓(0)이면 end 다음 문 수행 조건 : value relop value (값 관계연산자 값) DSRL
Control Flow • IF-END 조건 : 값 관계 연산자 값 DSRL
Control Flow • IF-END 논리 연산자(좀더 복잡한 점검을 위해 re loop와 같이 사용) DSRL
Control Flow • IF-ELSE-END if condition is ture statement t ; ….. end if condition is false statement f ; ….. end if condition statement t ; ….. else statement f ; ….. end DSRL
Control Flow • IF-ELSEIF-ELSE-END if condition_1 is ture statement t ; ….. end if condition_2 is ture statement s ; ….. end if condition_1 and condition_2 is false statement f ; ….. end if condition statement t ; ….. elseif statement s ; ….. else statement f ; ….. end DSRL
Control Flow • SWITCH-CASE SWITCH expression CASE value1 statement; . . CASE value2 statement; . . OTHERWISE statement; . . END DSRL
Control Flow • FOR Loops • Single FOR Loops FOR variable = expression statement statement . . . StatementEND 예) FOR i = 1 : 10 statement statement . . . StatementEND i 는 1 부터 시작해서 10까지 반복 for지수(index) = 초기치 : 증분 : 최종치 문장들end % 증분이 ‘1’인 경우 ‘:1’은 생략가능 DSRL
Control Flow • FOR Loops • Nested FOR Loops FOR variable_1 = expression_1 FORvariable_2 = expression_2 statement statement . . . StatementENDEND DSRL
Control Flow • FOR Loops • Special cases of the FOR Loop (Sweep through any fixed set of elements) for i=[1, 5, 99, -23, 4] fprintf(‘The value of I is %g.\n’,i); end for i=‘a’:’e’ fprintf(‘The value of I is %c.\n’,i); end DSRL
Control Flow • WHILE Loops WHILE condition statement1 statement2 . . .END 무한 루프일 경우 while 1 end i = 0 while i >= 0 statement end i = -5 while i <=0 i = i-1 statement end DSRL
FUNCTIONS • 함수의 일반적인 구조 • 1. 대형 프로그램 한 개를 작은 프로그램 몇 개로 분리 • 2. 자주 사용되는 임무를 수행하기 위해 사용 FUNCTION y = function_name(input arguments) % 함수사용에 도움을 주는 설명 % . . . 문장들 . . . y = something 함수명과 파일명을 같은 이름을 사용한다. 함수명 : function_name 파일명 : function_name.m 반환 값 y를 정함 DSRL
FUNCTIONS 함수 선언 함수 사용에 도움을 주는 설명 반환 값 DSRL
FUNCTIONS • RETURN문 DSRL
Exercises • 과제 • Guess 게임 프로그램을 작성하시오. Guess게임이란 추측게임으로 무작위로 1~10사이의 1개의 숫자 발생 시킨 다음 사용자로 하여금 그 숫자를 맞추는 게임이다. 그래서 사용자로 하여 총 3번의 기회가 주어지고 3번안에 맞추면 사용자 승 못하면 패가 되다. • 사용되는 MATLAB 함수 • rand() • input() • fprintf() DSRL
www.themegallery.com Thank You !