for loop array {commands} 예제 1.1 (For 반복변수의이용 ) >> data=[3 9 45 6; 7 16-1 5] data = 3 9 45 6 7 16-1 5 >> for n=data x=n(1)-n(2) -4-7 46 1 >> for n=1:10 x(n)=sin(n*pi/10); n=10; >> x Columns 1 through 7
0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090 Columns 8 through 10 0.5878 0.3090 0.0000 while expression {commands} 예제 1.2 (While 반복문의이용 ) >> num=0; EPS=1; >> while (1+EPS) > 1 EPS=EPS/2; num=num+1; >> num num = 53 >> EPS=2*EPS EPS = 2.2204e-16
예제 1.3 (For 와 If 를이용하여 While 문대치하기 ) >> EPS=1; % EPS 를수렴하는또다른방법을보여준다. >> for num=1:1000 EPS=EPS/2; if (1+EPS)<=1 EPS=EPS*2 break % break 는 loop 를빠져나가라는명령문이다. EPS = 2.2204e-16 >> num num = 53 예제 1.4 ( 반복문의다중이용 ) >> for n=1:5 for m=5:-1:1 A(n,m)=n^2+m^2; >> A A = 2 5 10 17 26 5 8 13 20 29 10 13 18 25 34 17 20 25 32 41 26 29 34 41 50
예제 1.5 ( 반복문의초기화 ) >> clear >> for n=1:10 % 배열의크기가자동으로 1 씩증가한다. x(n) = sin(n*pi/10); >> x=zeros(1,10); % 변수 x 를위한공간을미리할당한다. >> for n=1:10 x(n)=sin(n*pi/10); >> n, x % n 이 1 부터 10 까지, 마지막 문에이를때까지계산한다. n = 10 Columns 1 through 7 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090 Columns 8 through 10 0.5878 0.3090 0.0000 예제 1.6 (Loop 이용법보다효율적인배열이용법 ) >> n=1:10; >> x=sin(n*pi/10) Columns 1 through 7 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090 Columns 8 through 10 0.5878 0.3090 0.0000
예제 2.1 ( 단순문자열 ) >> x=-pi:.1:pi; y = sin(x); plot(x, y) >> title('sin(x)'), xlabel('x'), ylabel('y') >> t= How about this character string?' t = How about this character string? >> size(t) 1 32 >> v= I can''t find the manual!' % 문자열안의작은따옴표 (single quote) 는 % 를두번연속으로쓰는것으로나타낸다 v = I can't find the manual! 예제 2.2 ( 문자열의연산 ) >> u=abs(t); u=t+0 % 문자열에 0 을더해도역시 ASCII 표현으로바뀐다 u = Columns 1 through 12 72 111 119 32 27 98 111 117 116 32 116 104
Columns 13 through 24 105 115 32 99 104 97 114 97 99 116 101 114 Columns 25 through 32 32 115 116 114 105 110 103 63 >> v=setstr(u) v = How about this character string? >> u=t(16:24) % 문자열의 16 에서 24 번째원소는 character 라는단어이다 u = character >> u=t(24:-1:16) % character 의 spell 을거꾸로나열 u = retcarahc 예제 2.3 ( 문자열의합성 ) >> u= If a woodchuck could chuck wood,'; >> v= how much wood would a woodchuck chuck?'; >> w=[u v] w = If a woodchuck could chuck wood, how much wood would a woodchuck chuck? >> v=[ Character strings having more than ' one row must have the same number ' of columns just like matrices! '] v = Character strings having more than one row must have the same number of columns just like matrices! >> a= one' ; b= two' ; c= three' ; >> disp(str2mat(a, b, c, four')) % char() instead of str2mat() one two
three four 예제 2.4 ( 문자열변환 ) >> disp(u) If a woodchuck could chuck wood, >> i=find(u>= a' & u<= z'); % find is a very powerful function! >> u(i)=setstr(u(i)-( a'- A')) u = IF A WOODCHUCK COULD CHUCK WOOD, >> disp(v) Character strings having more than one row must have the same number of columns just like matrices! >> i=find(v>= a' & v<= z'); % here i is a single index vector into v, >> v(i)=setstr(v(i)-( a'- A')) % and matrix keeps the same orientation. v = CHARACTER STRINGS HAVING MORE THAN ONE ROW MUST HAVE THE SAME NUMBER OF COLUMNS JUST LIKE MATRICES!
예제 2.5 ( 숫자와문자열의변환 ) >> rad=2.5; area=pi*rad^2; >> t=[ A circle of radius ' num2str(rad) has an area of '... num2str(area).']; >> disp(t) % 함수 num2str 은숫자를열로바꾸는데사용된다. A circle of radius 2.5 has an area of 19.63. >> s= [1 2; pi 4]' % a string of a MATLAB matrix s = [1 2; pi 4] >> str2num(s) 1.0000 2.0000 3.1416 4.0000 >> s= 123e+5' % a string containing a simple number s = 123e+5 >> str2num(s) 12300000
예제 2.6 (printf 명령문 ) >> fprintf( See what this does') See what this does >> fprintf( See what this does\n') % \n 은새줄 (new line) 문자를뜻한다. See what this does >> t=sprintf( A circle of radius %.4g has an area of %.4g.',rad,area); >> disp(t) A circle of radius 2.5 has an area of 19.63. >> fprintf( A circle of radius %.4g has an area of %.4g.',rad,area) A circle of radius 2.5 has an area of 19.63.
예제 3.1 (input 함수 ) >> t= Enter number of rolls of tape > ' ; >> tape=input(t) Enter number of rolls of tape > 5 tape = 5 >> x=input( Enter anything > ', s') % 's' 는 MATLAB 에게입력값을문자열로서 % output 변수에전달하라는표현이다. Enter anything > anything can be entered anything can be entered 예제 3.2 (eval, feval 함수 ) >> a=eval( sqrt(2)') a = 1.4142 >> eval( a=sqrt(2)') % eval 함수를사용하는두가지경우의방법 a = 1.4142 >> eval( a=sqrtt(2), a=[]') % sqrtt 는 MATLAB 함수로유효하지않다. a = [] >> a=feval('sqrt',2) a = 1.4142
예제 3.3 (findstr 함수 ) >> b = Peter Piper picked a peck of pickled peppers'; >> findstr(b, ') % find spaces 6 12 19 21 26 29 37 >> findstr(b, p') % find the letter p 9 13 22 30 38 40 41 >> find(b== p') % for single character searches the find command works too 9 13 22 30 38 40 41
>> findstr(b, cow') % find the word cow [] >> findstr(b, pick') % find the string pick 13 30 예제 3.4 (strrep 함수 ) >> strrep(b, p', P') % capitalize all p's Peter PiPer Picked a Peck of Pickled PePPers >> strrep(b, Peter', Pamela') % change Peter to Pamela Pamela Piper picked a peck of pickled peppers 예제 3.5 (strtok 함수 ) >> disp(b) Peter Piper picked a peck of pickled peppers >> strtok(b) % find first token in above string separated by whitespace Peter >> [c,r]=strtok(b) % return the remainder of the string array in r c = Peter r = Piper picked a peck of pickled peppers >> [d,s]=strtok(b, pp') % let delimiter be lower or uppercase P d = eter s = Piper picked a peck of pickled peppers
Degree= 60 is Radian= 1.05 Degree=120 is Radian= 2.09... Degree=600 is Radian=10.47