|
|
|
Mathematics Laboratory |
|
The MathWorks |
|
www.mathworks.com |
|
|
|
|
|
|
Comments – “%” in first column |
|
Result Control - “;” at the end |
|
"x=5" |
|
x = |
|
5 |
|
"x=7;“ |
|
"x" |
|
x = |
|
7 |
|
|
|
|
|
|
Range Operator |
|
"x=3:2:13" |
|
x = |
|
3
5 7 9
11 13 |
|
Line Continuation - three periods |
|
Directory |
|
"dir" |
|
announ~2.doc mathview.doc matlab_6.doc
matlab~2.doc |
|
intro.m matlab.doc matlab~1.doc vector.m
announ~1.doc language.m
matlab1.doc matlab~1.ppt |
|
|
|
|
|
|
Print Working Directory |
|
"pwd" |
|
ans = |
|
E:\DOC\JEFF\COURSES\BEI\SHORTC~1\MATLAB |
|
Change Directory - cd |
|
"cd .." |
|
"pwd" |
|
ans = |
|
E:\DOC\JEFF\COURSES\BEI\SHORTC~1 |
|
|
|
|
|
|
"cd matlab" |
|
"pwd" |
|
ans = |
|
E:\DOC\JEFF\COURSES\BEI\SHORTC~1\MATLAB |
|
|
|
|
|
|
|
Creating a row vector |
|
"X=[1,2,3] or X=[1 2 3] |
|
X = |
|
1
2 3 |
|
Creating a column vector |
|
"Y=[1;2;3]" or "Y=[1
2
3]" |
|
Y = |
|
1 |
|
2 |
|
3 |
|
|
|
|
|
|
"Z=[y,2*y,3*y]" creates ? |
|
Z = |
|
1
2 3 |
|
2
4 6 |
|
3
6 9 |
|
|
|
|
|
|
|
"Z(2,3)=?" |
|
ans = |
|
6 |
|
"W=Z(1:2,2:3)" |
|
W = |
|
2 3 |
|
4 6 |
|
"W=Z(:,2)" |
|
W = |
|
2 |
|
4 |
|
6 |
|
|
|
|
|
|
|
"W=Z(2,:)" |
|
W = |
|
2
4 6 |
|
"Z(2,:)=[6,4,2] does ?" |
|
Z = |
|
1
2 3 |
|
6
4 2 |
|
3
6 9 |
|
"Z(2,:)=[ ] does ?" |
|
Z = |
|
1
2 3 |
|
3
6 9 |
|
|
|
|
|
|
|
|
|
|
A+B -
Addition, element by element |
|
"ZZ=[1,1,1;2,2,2]" |
|
ZZ = |
|
1
1 1 |
|
2
2 2 |
|
"W=Z+ZZ" |
|
W = |
|
2
3 4 |
|
5
8 11 |
|
|
|
|
|
|
|
A' -
Transpose |
|
A*B -
Matrix Multiplication |
|
"ZZ=ZZ' " |
|
ZZ = |
|
1 2 |
|
1 2 |
|
1 2 |
|
"W=Z*ZZ" |
|
W = |
|
6 12 |
|
18 36 |
|
|
|
|
|
|
|
A.*B - Element by Element Multiply |
|
Also: A/B, A\B, A./B, A.^2 |
|
Reserved Symbols |
|
Scalars: pi, i, j, inf, NaN, clock, date, ans |
|
"x=pi" |
|
x = |
|
3.1416 |
|
"x=i" |
|
x = |
|
0 + 1.0000i |
|
|
|
|
|
|
|
"zeros(3,5)" |
|
ans = |
|
0
0 0 0
0 |
|
0
0 0 0
0 |
|
0
0 0 0
0 |
|
|
|
|
|
|
|
|
"ones(5,3)" |
|
ans = |
|
1
1 1 |
|
1
1 1 |
|
1
1 1 |
|
1
1 1 |
|
1
1 1 |
|
|
|
|
|
|
"eye(4)" - the identity matrix |
|
ans = |
|
1
0 0 0 |
|
0
1 0 0 |
|
0
0 1 0 |
|
0
0 0 1 |
|
|
|
|
"<" - less than |
|
"<=" - less than or equal to |
|
">" - greater than |
|
">=" - greater than or equal to |
|
"==" - test for equality |
|
"~=" - not equal to |
|
"&" - AND |
|
"|"
- OR |
|
"~"
- NOT |
|
|
|
|
|
|
if x>5 & x<8 |
|
------- |
|
------- |
|
elseif(x>=8) |
|
------- |
|
------- |
|
else |
|
------- |
|
end |
|
|
|
|
|
for k=7:21 |
|
------- |
|
------- |
|
end |
|
|
|
|
|
k=1; |
|
while k=<10 |
|
------- |
|
------- |
|
k=k+1; |
|
end |
|