, , MATLAB , , . .
, . , , , , , .
, , , , for while, if, switch...case, try...catch, break, continue return. -, -.
.
, . , . ( ) M-: , for ( do FORTRAN), , , while. ( ), , M-.
for :
for var=
end
var (, ) ; var , , a1:a2:a3, .. a1 a3 a2 ( a2 , 1 FORTRAN Basic ), var a3; , (,) (;), end.
for.
for x=1:3:10
y=x/4
end
:
y =
0.2500
y =
y =
1.7500
y =
2.5000
.
.. 57
s=0
for k=1:1:57
s=s+1./k^2;
end;
( ) -, :
|
|
for k=[2 5 7 8 11 15 25]
, k 2, 5, 7, 8, 11, 15 25.
, :
A=[1 2; 3 4];
for k=A
A, .. . , .. -. , , , A:
S=0; A=[1 2; 3 4];
for k=A
s=s+sqrt(k(1)^2+k(2)^2);
end;
while :
while
end
, ; while , for end; MATLAB , .
12 0 11 while:
i=1; t=0;
while t<=11
y(i)=sin(t)
i=i+1;
t=t+1;
end
.
:
s=0; k=1; u=1;
while u>1.e-8
s=s+u;
k=k+1;
u=1./k^2;
end
( ): , , while, .
, , , , , Enter . Enter , (>>) . , , MATLAB , ( ). Enter. end MATLAB . , , >>.
for while break, . , . while 1. break , .
continue , .. for while, , . continue , .
|
|
MATLAB , , M-, , , . , ,
k=0;
for x=0:0.1:100
k=k+1;
y(k)=sin(x);
end;
x=0:0.1:100;
y=sin(x);
M- [7,69-71,109,132,177].
.
if switch, , -
.
if, if (), else (), elseif ( ), end ( ).
if :
if
end
( ), (, MATLAB , , , ( ) , , , ).
A=[1 2; 3 0];
if A
b=1;
else
b=2;
, b 2, , A , . , if A if A~=0 if all(A(:)).
, , , , , end. end. if :
if _1
_1
elseif _2
_2
elseif _3
_3
...
else
end
, elseif, _1, _1, _2, _2 .. , , else. , elseif , elseif . end.
, if...else...elseif...end - temperature ( temperature.m):
function temperature(t)
if(t<=0)
disp(' ')
elseif and(t>0, t<=10)
disp(' ')
elseif and(t>10, t<=18)
disp(' ')
elseif and(t>18, t<=25)
disp(' ')
else
disp(' ')
end
- , temperature. , , :
>> temperature(-30)
>> temperature(5)
>> temperature(11)
>> temperature(21)
>> temperature(27)
|
|
break for while. return. , , - . - factorial ( factorial.m):
function res=factorial(n)
res=1;
if n==1
return
else
for i=2:n
res=res.*i;
end
end
, return , (res=1;) .
return , , .
break, , . , , :
prod=1; i=1;
while 1
prod=prod.*i
i=i+1;
if i>9
break
end
end
, break, if, . , , ( ).
, , switch, if. switch ( FORTRAN SELECT CASE):
switch
case _1
case _2
case {_3, _4,...}
...
otherwise
end
, , ( ). _1, _2, _3, _4 .. ( ). , , case. , otherwise end. case , otherwise . {_3, _4,...} , case ( case , ).
, , , case , strcmp(,) 1 ().
switch MATLAB: switch , , , ( , , C/C++).
- seasons ( seasons.m) switch:
switch lower(s)
case{'','',''}
disp(' - ')
case{'','',''}
disp(' - ')
case{'','',''}
disp(' - ')
|
|
case{'','',''}
disp(' - ')
otherwise
disp(' ')
end
, , s (, lower(s) s ). s , , . , :
>> s='';
>> seasons
-
>> s='';
>> seasons
-
>> s='';
>> seasons
-
>> s='';
>> seasons
-
>> s='125';
>> seasons
.
, MATLAB, , , , , . , try...catch , ( , , ), , (.. (, ) ).
, , try...catch, ( ).
try...catch :
try
_1
catch
_2
end
. _1, - (, , , , ), _2 (.. _2 , _1 ( , , ).
try...catch -, :
function MatMult(A,B)
try
X=A*B
catch
disp(' A B ')
end
, , , , - , catch, , disp, .
>> A=[6 5 4; 3 2 1];
>> B=[1 2 3; 4 5 6; 2 3 4];
>> MatMult(A,B)
X =
34 49 64
13 19 25
>> MatMult(B,A)
A B
, , try...catch , , , ( , ), . , catch-, , MATLAB , .
.
, , () , . . , , , , . , . , , , MATLAB, , - (, , , , , ..). , , , , - .
|
|
, , , , - :
( );
( ) ( ) ;
( ).
. ( MATLAB ) ().
, , . , . , , . , , , , , , .
. , , , , .
, , , ( ). , , 16 , 8 .
, , , , , . , , , , , ( ).
, . - . . , , , , . , , , , .