예제 1.1 ( 관계연산자 ) >> A=1:9, B=9-A A = 1 2 3 4 5 6 7 8 9 B = 8 7 6 5 4 3 2 1 0 >> tf = A>4 % 4 보다큰 A 의원소들을찾을경우 tf = 0 0 0 0 1 1 1 1 1 >> tf = (A==B) % A 의원소와 B 의원소가똑같은경우를찾을때
tf = 0 0 0 0 0 0 0 0 0 >> tf = B-(A>2) % 관계연산자의결과가 0 과 1 의수치배열이기때문에 % 수학적인연산 (+, -,..., etc) 안에서사용될수있다. tf = 8 7 5 4 3 2 1 0-1 예제 1.2 (== 관계를이용한예외의처리 ) >> x = [-3:3]/3 x = -1.0000-0.6667-0.3333 0 0.3333 0.6667 1.0000 >> sin(x)./x % sin(0)/0 은정의되지않으므로 MATLAB 은그결과로서 % 5 번째위치에 NaN(Not-a-Number) 을출력한다. Warning : Divide by zero ans = 0.8415 0.9276 0.9816 NaN 0.9816 0.9276 0.8415 >> x = x + (x==0)*eps; % x=0인경우 sin(eps)/eps=1 이라는극한값을준다. >> sin(x)./x ans = 0.8415 0.9276 0.9816 1.0000 0.9816 0.9276 0.8415 예제 1.3 ( 관계연산자를이용한불연속함수의생성 ) >> x = linspace(0,10,100); % create data >> y = sin(x); % compute sine >> z = (y>=0).*y % set negative values of sin(x) to zero >> z = z + 0.5*(y<0); % where sin(x) is negative add 1/2 >> z = (x<=8).*z % set value past x=8 to zero >> plot(x,z), xlabel('x'), ylabel('z=f(x)'), title('a Discontinuous Signal')
1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 1 2 3 4 5 6 7 8 9 10 예제 1.4 ( 논리연산자 ) >> A=1:9; B=9-A; >> tf = A>4 tf = 0 0 0 0 1 1 1 1 1 >> tf = ~(A>4) tf = 1 1 1 1 0 0 0 0 0 >> tf = (A>2) & (A<6) tf = 0 0 1 1 1 0 0 0 0
예제 2.1 (NaN 의연산 ) >> a=[1 2 nan inf nan] % note, in use, NaN can be lowercase a = 1 2 NaN Inf NaN >> b=2*a b = 2 4 NaN Inf NaN >> c=sqrt(a) c = 1.0000 1.4142 NaN Inf NaN >> d = (a==nan) d = 0 0 0 0 0 >> f = (a~=nan) f = 1 1 1 1 1 >> g = (a~=a) % NaN은자기자신과다른유일한원소이다. g = 0 0 1 0 1 >> g = isnan(a) g = 0 0 1 0 1 >> i=find(isnan(a)) % find indices of NaN i = 3 5 >> a(i)=zeros(size(i)) % changes NaNs in a to zeros a = 1 2 0 Inf 0 예제 2.2 ( 공행렬의연산 ) >> x=(1:5)-3 % new data
x = -2-1 0 1 2 >> y=find(x>2) y = [] >> isempty(y) ans = 1 >> find(y==0) ans = [] if expression {commands}
예제 3.1 ( 조건분기명령문 ) >> apples=10; % number of apples >> cost=apples*25 % cost of apples cost = 250 >>if apples>5 % give 20% discount cost = cost*(1-20/100); >> cost cost = 200 if expression commands evaluated if expression is True else commands evaluated if expression is False if expression1 commands evaluated if expression1 is True elseif else commands evaluated if no other expression is True
예제 4.1 (For 반복문의이용 ) >> clear >> sum = 0 % sum=0 이없으면, []+n=[] 가되어원하는결과를얻을수없게된다. >> for n=1:10 sum = sum + n; >> sum 55 예제 4.2 ( 다중반복문의이용 ) >> A = zeros(9,9); >> for i = 1:9 for j = 1:9 if i>j; A(i,j) = i*j; if j>i; A(i,j) = i+j; >> A A = 0 3 4 5 6 7 8 9 10 2 0 5 6 7 8 9 10 11 3 6 0 7 8 9 10 11 12 4 8 12 0 9 10 11 12 13 5 10 15 20 0 11 12 13 14 6 12 18 24 30 0 13 14 15 7 14 21 28 35 42 0 15 16 8 16 24 32 40 48 56 0 17 9 18 27 36 45 54 63 72 0 while expression {commands}
예제 5.1 (MATLAB M-file example.m) x = linspace(-pi, pi, 80); y = sin(x); plot(x,y) >> example % -------- example.m 의실행
예제 5.2 (MATLAB function-file fliplr.m) function y = fliplr(x) %FLIPLR Flip matrix in the lefr/right direction. % FLIPLR(X) returns X with row preserved and % columns flopped in the lerft/right direction. % % X = 1 2 3 becomes 3 2 1 % 4 5 6 6 5 4 % % See also FLIPUD, ROT90. % Copyright (c) 1984-93 by The MathWorks, Inc. [m, n] = size(x); y = x(:, n:-1:1); 예제 5.3 (MATLAB function-file linspace.m) function y = linspace(d1, d2, n) %LINSPACE Linearly spaced vector. % LINSPACE(x1, x2) generates a row vector of 100 % linearly equally spaced points between x1 and x2. % LINSPACE(x1, x2, N) generates N points
% between x1 and x2. % % See also LOGSPACE, :. % Copyright (c) 1984-94 by The MathWorks, Inc. if nargin == 2 n = 100; y = [d1+(0:n-2)*(d2-d1)/(n-1) d2];
sin