📄 cs143 introduction to matlab (code).mht
字号:
(second argument specifies</SPAN>
<SPAN class=3Dcomment>% dimension along =
which operation is taken)</SPAN>
[1 2 3] * [4 5 6]' <SPAN class=3Dcomment>% 1x3 row vector =
times a 3x1 column vector</SPAN>
<SPAN class=3Dcomment>% results in a =
scalar. Known as dot product</SPAN>
<SPAN class=3Dcomment>% or inner product. =
Note the absence of "."</SPAN>
[1 2 3]' * [4 5 6] <SPAN class=3Dcomment>% 3x1 column vector =
times a 1x3 row vector </SPAN>
<SPAN class=3Dcomment>% results in a 3x3 =
matrix. Known as outer</SPAN>
<SPAN class=3Dcomment>% product. Note =
the absence of "."</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (C) Matrix Operations:</SPAN>
a =3D rand(3,2) <SPAN class=3Dcomment>% A 3x2 =
matrix</SPAN>
b =3D rand(2,4) <SPAN class=3Dcomment>% A 2x4 =
matrix</SPAN>
c =3D a * b <SPAN class=3Dcomment>% Matrix product =
results in a 3x4 matrix</SPAN>
a =3D [1 2; 3 4; 5 6]; <SPAN class=3Dcomment>% A 3x2 =
matrix</SPAN>
b =3D [5 6 7]; <SPAN class=3Dcomment>% A 1x3 row =
vector</SPAN>
b * a <SPAN class=3Dcomment>% Vector-matrix =
product results in</SPAN>
<SPAN class=3Dcomment>% a 1x2 row =
vector</SPAN>
c =3D [8; 9]; <SPAN class=3Dcomment>% A 2x1 column =
vector</SPAN>
a * c <SPAN class=3Dcomment>% Matrix-vector =
product results in</SPAN>
<SPAN class=3Dcomment>% a 3x1 column =
vector</SPAN>
a =3D [1 3 2; 6 5 4; 7 8 9]; <SPAN class=3Dcomment>% A 3x3 =
matrix</SPAN>
inv(a) <SPAN class=3Dcomment>% Matrix inverse of =
a</SPAN>
eig(a) <SPAN class=3Dcomment>% Vector of =
eigenvalues of a</SPAN>
[V, D] =3D eig(a) <SPAN class=3Dcomment>% D matrix with =
eigenvalues on diagonal;</SPAN>
<SPAN class=3Dcomment>% V matrix of =
eigenvectors</SPAN>
<SPAN class=3Dcomment>% Example for =
multiple return values!</SPAN>
[U, S, V] =3D svd(a) <SPAN class=3Dcomment>% Singular value =
decomposition of a.</SPAN>
<SPAN class=3Dcomment>% a =3D U * S * V', =
singular values are</SPAN>
<SPAN class=3Dcomment>% stored in =
S</SPAN>
<SPAN class=3Dcomment>% Other matrix operations: det, norm, rank, =
...</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (D) Reshaping and assembling matrices:</SPAN>
a =3D [1 2; 3 4; 5 6]; <SPAN class=3Dcomment>% A 3x2 =
matrix</SPAN>
b =3D a(:) <SPAN class=3Dcomment>% Make 6x1 column =
vector by stacking </SPAN>
<SPAN class=3Dcomment>% up columns of =
a</SPAN>
sum(a(:)) <SPAN class=3Dcomment>% Useful: sum of all =
elements</SPAN>
a =3D reshape(b, 2, 3) <SPAN class=3Dcomment>% Make 2x3 matrix =
out of vector </SPAN>
<SPAN class=3Dcomment>% elements =
(column-wise)</SPAN>
a =3D [1 2]; b =3D [3 4]; <SPAN class=3Dcomment>% Two row =
vectors</SPAN>
c =3D [a b] <SPAN class=3Dcomment>% Horizontal =
concatenation (see horzcat)</SPAN>
a =3D [1; 2; 3]; <SPAN class=3Dcomment>% Column =
vector</SPAN>
c =3D [a; 4] <SPAN class=3Dcomment>% Vertical =
concatenation (see vertcat)</SPAN>
a =3D [eye(3) rand(3)] <SPAN class=3Dcomment>% Concatenation for =
matrices</SPAN>
b =3D [eye(3); ones(1, 3)]
b =3D repmat(5, 3, 2) <SPAN class=3Dcomment>% Create a 3x2 =
matrix of fives</SPAN>
b =3D repmat([1 2; 3 4], 1, 2) <SPAN class=3Dcomment>% Replicate the 2x2 =
matrix twice in</SPAN>
<SPAN class=3Dcomment>% column direction; =
makes 2x4 matrix</SPAN>
b =3D diag([1 2 3]) <SPAN class=3Dcomment>% Create 3x3 =
diagonal matrix with given</SPAN>
<SPAN class=3Dcomment>% diagonal =
elements</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (4) Control statements & =
vectorization</SPAN>
<SPAN class=3Dcomment>% Syntax of control flow statements:</SPAN>
<SPAN class=3Dcomment>% </SPAN>
<SPAN class=3Dcomment>% for VARIABLE =3D EXPR</SPAN>
<SPAN class=3Dcomment>% STATEMENT</SPAN>
<SPAN class=3Dcomment>% ...</SPAN>
<SPAN class=3Dcomment>% STATEMENT</SPAN>
<SPAN class=3Dcomment>% end </SPAN>
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% EXPR is a vector here, e.g. 1:10 or -1:0.5:1 =
or [1 4 7]</SPAN>
<SPAN class=3Dcomment>% </SPAN>
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% while EXPRESSION</SPAN>
<SPAN class=3Dcomment>% STATEMENTS</SPAN>
<SPAN class=3Dcomment>% end</SPAN>
<SPAN class=3Dcomment>% </SPAN>
<SPAN class=3Dcomment>% if EXPRESSION</SPAN>
<SPAN class=3Dcomment>% STATEMENTS </SPAN>
<SPAN class=3Dcomment>% elseif EXPRESSION</SPAN>
<SPAN class=3Dcomment>% STATEMENTS</SPAN>
<SPAN class=3Dcomment>% else</SPAN>
<SPAN class=3Dcomment>% STATEMENTS</SPAN>
<SPAN class=3Dcomment>% end </SPAN>
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% (elseif and else clauses are optional, the =
"end" is required)</SPAN>
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% EXPRESSIONs are usually made of relational =
clauses, e.g. a < b</SPAN>
<SPAN class=3Dcomment>% The operators are <, >, <=3D, =
>=3D, =3D=3D, ~=3D (almost like in C(++))</SPAN>
<SPAN class=3Dcomment>% Warning:</SPAN>
<SPAN class=3Dcomment>% Loops run very slowly in Matlab, because of =
interpretation overhead.</SPAN>
<SPAN class=3Dcomment>% This has gotten somewhat better in version =
6.5, but you should</SPAN>
<SPAN class=3Dcomment>% nevertheless try to avoid them by =
"vectorizing" the computation, </SPAN>
<SPAN class=3Dcomment>% i.e. by rewriting the code in form of matrix =
operations. This is</SPAN>
<SPAN class=3Dcomment>% illustrated in some examples below.</SPAN>
<SPAN class=3Dcomment>% Examples:</SPAN>
<SPAN class=3Dkeyword>for</SPAN> i=3D1:2:7 <SPAN =
class=3Dcomment>% Loop from 1 to 7 in steps of 2</SPAN>
i <SPAN class=3Dcomment>% Print i</SPAN>
<SPAN class=3Dkeyword>end</SPAN>
<SPAN class=3Dkeyword>for</SPAN> i=3D[5 13 -1] <SPAN =
class=3Dcomment>% Loop over given vector</SPAN>
<SPAN class=3Dkeyword>if</SPAN> (i > 10) <SPAN =
class=3Dcomment>% Sample if statement</SPAN>
disp(<SPAN class=3Dstring>'Larger than 10'</SPAN>) <SPAN =
class=3Dcomment>% Print given string</SPAN>
<SPAN class=3Dkeyword>elseif</SPAN> i < 0 <SPAN =
class=3Dcomment>% Parentheses are optional</SPAN>
disp(<SPAN class=3Dstring>'Negative value'</SPAN>)=20
<SPAN class=3Dkeyword>else</SPAN>
disp(<SPAN class=3Dstring>'Something else'</SPAN>)
<SPAN class=3Dkeyword>end</SPAN>
<SPAN class=3Dkeyword>end</SPAN>
<SPAN class=3Dcomment>% Here is another example: given an mxn matrix A =
and a 1xn </SPAN>
<SPAN class=3Dcomment>% vector v, we want to subtract v from every row =
of A.</SPAN>
m =3D 50; n =3D 10; A =3D ones(m, n); v =3D 2 * rand(1, n);=20
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% Implementation using loops:</SPAN>
<SPAN class=3Dkeyword>for</SPAN> i=3D1:m
A(i,:) =3D A(i,:) - v;
<SPAN class=3Dkeyword>end</SPAN>
<SPAN class=3Dcomment>% We can compute the same thing using only matrix =
operations</SPAN>
A =3D ones(m, n) - repmat(v, m, 1); <SPAN class=3Dcomment>% This =
version of the code runs </SPAN>
<SPAN class=3Dcomment>% much =
faster!!!</SPAN>
<SPAN class=3Dcomment>% We can vectorize the computation even when loops =
contain</SPAN>
<SPAN class=3Dcomment>% conditional statements.</SPAN>
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% Example: given an mxn matrix A, create a matrix =
B of the same size</SPAN>
<SPAN class=3Dcomment>% containing all zeros, and then copy into B the =
elements of A that</SPAN>
<SPAN class=3Dcomment>% are greater than zero.</SPAN>
<SPAN class=3Dcomment>% Implementation using loops:</SPAN>
B =3D zeros(m,n);
<SPAN class=3Dkeyword>for</SPAN> i=3D1:m
<SPAN class=3Dkeyword>for</SPAN> j=3D1:n
<SPAN class=3Dkeyword>if</SPAN> A(i,j)>0
B(i,j) =3D A(i,j);
<SPAN class=3Dkeyword>end</SPAN>
<SPAN class=3Dkeyword>end</SPAN>
<SPAN class=3Dkeyword>end</SPAN>
<SPAN class=3Dcomment>% All this can be computed w/o any loop!</SPAN>
B =3D zeros(m,n);
ind =3D find(A > 0); <SPAN class=3Dcomment>% Find indices =
of positive elements of A </SPAN>
<SPAN class=3Dcomment>% (see "help find" =
for more info)</SPAN>
B(ind) =3D A(ind); <SPAN class=3Dcomment>% Copies into B =
only the elements of A</SPAN>
<SPAN class=3Dcomment>% that are > =
0</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>%(5) Saving your work</SPAN>
save myfile <SPAN class=3Dcomment>% Saves all workspace =
variables into</SPAN>
<SPAN class=3Dcomment>% file =
myfile.mat</SPAN>
save myfile a b <SPAN class=3Dcomment>% Saves only =
variables a and b</SPAN>
clear a b <SPAN class=3Dcomment>% Removes variables a =
and b from the</SPAN>
<SPAN class=3Dcomment>% workspace</SPAN>
clear <SPAN class=3Dcomment>% Clears the entire =
workspace</SPAN>
load myfile <SPAN class=3Dcomment>% Loads variable(s) =
from myfile.mat</SPAN>
=20
=20
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>%(6) Creating scripts or functions using m-files: =
</SPAN>
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% Matlab scripts are files with ".m" extension =
containing Matlab </SPAN>
<SPAN class=3Dcomment>% commands. Variables in a script file are global =
and will change the</SPAN>
<SPAN class=3Dcomment>% value of variables of the same name in the =
environment of the current</SPAN>
<SPAN class=3Dcomment>% Matlab session. A script with name "script1.m" =
can be invoked by</SPAN>
<SPAN class=3Dcomment>% typing "script1" in the command window.</SPAN>
<SPAN class=3Dcomment>% Functions are also m-files. The first line in a =
function file must be</SPAN>
<SPAN class=3Dcomment>% of this form: </SPAN>
<SPAN class=3Dcomment>% function [outarg_1, ..., outarg_m] =3D =
myfunction(inarg_1, ..., inarg_n)</SPAN>
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% The function name should be the same as that of =
the file </SPAN>
<SPAN class=3Dcomment>% (i.e. function "myfunction" should be saved in =
file "myfunction.m"). </SPAN>
<SPAN class=3Dcomment>% Have a look at myfunction.m and =
myotherfunction.m for examples.</SPAN>
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% Functions are executed using local workspaces: =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -