Professional Documents
Culture Documents
MATLAB
MATLAB
(IPT)
Matlab
Workspace
Command Window
>>
Command History
(cont.)
Current Directory Window
C:\Users\..\Documents\MATLAB
M
command window
[Enter]
Matlab
ex: 2*3-4
2 * 3 - 4
ex: imre IMRE
MATLAB
MATLAB (+ - * / ^)
>> Enter
>> 2 + 2
ans =
4
format
>> format long
%
>> 11/7
ans=
1.571428571428571
>>format
>> sqrt(2)
>> sin(pi/8)
>> log(10)
MATLAB
abs(x)
sqrt(x)
round(x)
fix(x)
floor(x)
ceil(x)
sign(x)
1
rem(x,y)
gcd(x,y)
lcm(x,y)
exp(x)
pow2(x)
log(x)
log2(x)
log10(x)
2 (2X)
e
2
10
MATLAB
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x)
atan2(x,y)
sinh(x)
cosh(x)
tanh(x)
asinh(x)
acosh(x)
atanh(x)
(_)
63 MATLAB
:
a = 5 ^ (7 /2)
a=
279.5085
(View )
whos
MATLAB
MATLAB
1X1
---
a(2,3)
% 2 3
MATLAB
a(10)
% a(2,3)
r c M m(i,j)
m(i + r*(j-1))
a([1 6 11 16])
MATLAB ( )
a b
% a b
( )
a ib
% a i b
linspace
x = linspace(4, 10, 6) % 4, 10,
6
a(2,1:3)
a(2:4,3)
a(2:3,3:4)
a(3,:)
a(:,2)
a(:) % (
)
end
v(end:1) %
v(end:-2:1) %
2
a(end,end)
a(end, end-2)
a(2:end,end:-2:1)
E=a([1 3],[2 3])
MATLAB
Operator
Name
Comments and
Examples
+
Array and matrix addition
a + b, A + B, or a + A.
.*
Array multiplication
Cv= A.*B, C(I, J) =
A(I, J)*B(I,
J).
*
Matrix multiplication
A*B, standard matrix
multiplication.
./
Array right division
C = A./B, C(I, J) =
A(I, J)/B(I, J).
.\
Array left division
C = A.\B, C(I, J) =
B(I, J)/A(I, J).
/
Matrix right division
A/B is the preferred way
to compute A*inv(B).
Operator
Name
Examples
\
Matrix left division
.^
Array power
Comments and
A\B is the preferred way to
compute inv(A)*B.
If C = A.^B, then C(I, J)
A(I,
J)^B(I, J).
.'
matrix transpose
transpose.
'
matrix transpose
+
Unary plus
Unary minus
A or 1*A.
inv
inv(a)
flipud fliplr
flipud(a)
fliplr(a)
reshape
reshape(a, 2, 8)
a=[1 2;3 4]
b=[2 3;4 5]
a.*b
% a b
ans =
2
6
12 20
MATLAB
0 1 zeros ones
zeros(n)
zeros(m,n)
zeros(a)
rand randn
rand [0, 1]
randn 0 1
floor(10*rand(3))
floor(100*randn(3,5))
meshgrid 2D 3D
[rows cols]=meshgrid(1:10,1:10)
size
size(a)
If Else
:if (True or False), , end
ex: if (isempty(t))
t = 0;
End
if (True_or_False), , else,
, end
switch
:switch (switch_expr)
case case_expr_1
statement1
otherwise
statement1
end
ex: switch (value)
case {1 ,3 ,5 ,7 , 9}
disp(the value is odd.);
case {2 ,4 , 6, 8, 10}
disp(the value is even.);
otherwise
disp(the value is out of range.);
end
For loop
for = , , end
ex: for i = 1:100,
ex: for
i=1:2:100 ,
y(i) =2;
s = s+i;
End
end
ex: for x = [0 1 5 3],
disp(x); // x
end
While loop
while expression , , end
ex: i=1;
ex:
i=1
while i <= 100
while i <=
100
y(i) =2;
s = s+i;
i=i+1;
i=i+2;
end
end
MATLAB
i=1:10^6;sin(i);
10
[1:10].^2
a :
a>0
plot()
plot(x,y) x y
ex:
x=[0 :0.1: 2*pi];
plot(x,sin(x))
0 2 0.1 sin
plot(x,sin(x),.,x,cos(x),o)
MATLAB
MATLAB (script)
script.m
script
MATLAB (function)
MATLAB ( )
sum(a) %
sum(a>0) % 0
sum(a(:) > 0) % 0
MATLAB
--- function
(lookfor)
(help)
function num=countpos(a)
% countpos finds the number of positive
% elementsin a matrix.
% usage:
% n=countpos(a)
num=sum(a(: )>0);
http://libai.math.ncu.edu.tw/bcc16/B
/matlab/index.shtml