📄 cs143 introduction to matlab (code).mht
字号:
From: <Saved by Windows Internet Explorer 7>
Subject: CS143: Introduction to Matlab (Code)
Date: Tue, 12 May 2009 09:39:13 -0700
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_0000_01C9D2E5.82990560"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
This is a multi-part message in MIME format.
------=_NextPart_000_0000_01C9D2E5.82990560
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.cs.brown.edu/courses/cs143/MatlabTutorialCode.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>CS143: Introduction to Matlab (Code)</TITLE>
<META content=3D"MSHTML 6.00.6000.16825" name=3DGENERATOR>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1"><LINK=20
rev=3Dmade href=3D"mailto: roth (AT) cs DOT brown DOT edu"><LINK=20
href=3D"http://www.cs.brown.edu/courses/cs143/plain.css" type=3Dtext/css =
rel=3Dstylesheet></HEAD>
<BODY>
<H1>CS143: Introduction to Matlab (Code)</H1>
<H2>intro.m</H2><A id=3Dintro name=3Dintro></A><PRE class=3Dmcode><SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% Introduction to Matlab </SPAN>
<SPAN class=3Dcomment>% (adapted from =
http://www.stanford.edu/class/cs223b/matlabIntro.html)</SPAN>
<SPAN class=3Dcomment>%</SPAN>
<SPAN class=3Dcomment>% Stefan Roth <roth (AT) cs DOT brown DOT =
edu>, 09/08/2003</SPAN>
<SPAN class=3Dcomment>% Last modified: 09/10/2003</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (1) Basics</SPAN>
<SPAN class=3Dcomment>% The symbol "%" is used to indicate a comment =
(for the remainder of</SPAN>
<SPAN class=3Dcomment>% the line).</SPAN>
<SPAN class=3Dcomment>% When writing a long Matlab statement that =
becomes to long for a</SPAN>
<SPAN class=3Dcomment>% single line use "..." at the end of the line to =
continue on the next</SPAN>
<SPAN class=3Dcomment>% line. E.g.</SPAN>
A =3D [1, 2; <SPAN class=3Dcont>...</SPAN>
3, 4];
<SPAN class=3Dcomment>% A semicolon at the end of a statement means that =
Matlab will not</SPAN>
<SPAN class=3Dcomment>% display the result of the evaluated statement. =
If the ";" is omitted</SPAN>
<SPAN class=3Dcomment>% then Matlab will display the result. This is =
also useful for</SPAN>
<SPAN class=3Dcomment>% printing the value of variables, e.g.</SPAN>
A
<SPAN class=3Dcomment>% Matlab's command line is a little like a =
standard shell:</SPAN>
<SPAN class=3Dcomment>% - Use the up arrow to recall commands without =
retyping them (and </SPAN>
<SPAN class=3Dcomment>% down arrow to go forward in the command =
history). </SPAN>
<SPAN class=3Dcomment>% - C-a moves to beginning of line (C-e for end), =
C-f moves forward a</SPAN>
<SPAN class=3Dcomment>% character and C-b moves back (equivalent to =
the left and right</SPAN>
<SPAN class=3Dcomment>% arrow keys), C-d deletes a character, C-k =
deletes the rest of the</SPAN>
<SPAN class=3Dcomment>% line to the right of the cursor, C-p goes back =
through the</SPAN>
<SPAN class=3Dcomment>% command history and C-n goes forward =
(equivalent to up and down</SPAN>
<SPAN class=3Dcomment>% arrows), Tab tries to complete a =
command.</SPAN>
<SPAN class=3Dcomment>% Simple debugging:</SPAN>
<SPAN class=3Dcomment>% If the command "dbstop if error" is issued =
before running a script</SPAN>
<SPAN class=3Dcomment>% or a function that causes a run-time error, the =
execution will stop</SPAN>
<SPAN class=3Dcomment>% at the point where the error occurred. Very =
useful for tracking down</SPAN>
<SPAN class=3Dcomment>% errors.</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (2) Basic types in Matlab</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (A) The basic types in Matlab are scalars =
(usually double-precision</SPAN>
<SPAN class=3Dcomment>% floating point), vectors, and matrices:</SPAN>
A =3D [1 2; 3 4]; <SPAN class=3Dcomment>% Creates a 2x2 =
matrix</SPAN>
B =3D [1,2; 3,4]; <SPAN class=3Dcomment>% The simplest way =
to create a matrix is</SPAN>
<SPAN class=3Dcomment>% to list its entries =
in square brackets.</SPAN>
<SPAN class=3Dcomment>% The ";" symbol =
separates rows;</SPAN>
<SPAN class=3Dcomment>% the (optional) "," =
separates columns.</SPAN>
N =3D 5 <SPAN class=3Dcomment>% A scalar</SPAN>
v =3D [1 0 0] <SPAN class=3Dcomment>% A row =
vector</SPAN>
v =3D [1; 2; 3] <SPAN class=3Dcomment>% A column =
vector</SPAN>
v =3D v' <SPAN class=3Dcomment>% Transpose a =
vector (row to column or </SPAN>
<SPAN class=3Dcomment>% column to =
row)</SPAN>
v =3D 1:.5:3 <SPAN class=3Dcomment>% A vector filled =
in a specified range: </SPAN>
v =3D pi*[-4:4]/4 <SPAN class=3Dcomment>% =
[start:stepsize:end]</SPAN>
<SPAN class=3Dcomment>% (brackets are =
optional)</SPAN>
v =3D [] <SPAN class=3Dcomment>% Empty =
vector</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (B) Creating special matrices: 1ST parameter is =
ROWS,</SPAN>
<SPAN class=3Dcomment>% 2ND parameter is COLS </SPAN>
m =3D zeros(2, 3) <SPAN class=3Dcomment>% Creates a 2x3 =
matrix of zeros</SPAN>
v =3D ones(1, 3) <SPAN class=3Dcomment>% Creates a 1x3 =
matrix (row vector) of ones</SPAN>
m =3D eye(3) <SPAN class=3Dcomment>% Identity matrix =
(3x3)</SPAN>
v =3D rand(3, 1) <SPAN class=3Dcomment>% Randomly filled =
3x1 matrix (column </SPAN>
<SPAN class=3Dcomment>% vector); see also =
randn</SPAN>
<SPAN class=3Dcomment>% But watch =
out:</SPAN>
m =3D zeros(3) <SPAN class=3Dcomment>% Creates a 3x3 =
matrix (!) of zeros</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (C) Indexing vectors and matrices.</SPAN>
<SPAN class=3Dcomment>% Warning: Indices always start at 1 and *NOT* at =
0!</SPAN>
v =3D [1 2 3];
v(3) <SPAN class=3Dcomment>% Access a vector =
element </SPAN>
m =3D [1 2 3 4; 5 7 8 8; 9 10 11 12; 13 14 15 16]
m(1, 3) <SPAN class=3Dcomment>% Access a matrix =
element</SPAN>
<SPAN class=3Dcomment>% matrix(ROW #, =
COLUMN #)</SPAN>
m(2, :) <SPAN class=3Dcomment>% Access a whole =
matrix row (2nd row)</SPAN>
m(:, 1) <SPAN class=3Dcomment>% Access a whole =
matrix column (1st column)</SPAN>
m(1, 1:3) <SPAN class=3Dcomment>% Access elements 1 =
through 3 of the 1st row</SPAN>
m(2:3, 2) <SPAN class=3Dcomment>% Access elements 2 =
through 3 of the </SPAN>
<SPAN class=3Dcomment>% 2nd column</SPAN>
m(2:<SPAN class=3Dkeyword>end</SPAN>, 3) <SPAN =
class=3Dcomment>% Keyword "end" accesses the remainder of a</SPAN>
<SPAN class=3Dcomment>% column or =
row</SPAN>
m =3D [1 2 3; 4 5 6] =20
size(m) <SPAN class=3Dcomment>% Returns the size of =
a matrix</SPAN>
size(m, 1) <SPAN class=3Dcomment>% Number of =
rows</SPAN>
size(m, 2) <SPAN class=3Dcomment>% Number of =
columns</SPAN>
m1 =3D zeros(size(m)) <SPAN class=3Dcomment>% Create a new =
matrix with the size of m</SPAN>
who <SPAN class=3Dcomment>% List variables in =
workspace</SPAN>
whos <SPAN class=3Dcomment>% List variables w/ =
info about size, type, etc.</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (3) Simple operations on vectors and =
matrices</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (A) Element-wise operations:</SPAN>
<SPAN class=3Dcomment>% These operations are done "element by element". =
If two </SPAN>
<SPAN class=3Dcomment>% vectors/matrices are to be added, subtracted, or =
element-wise</SPAN>
<SPAN class=3Dcomment>% multiplied or divided, they must have the same =
size.</SPAN>
a =3D [1 2 3 4]'; <SPAN class=3Dcomment>% A column =
vector</SPAN>
2 * a <SPAN class=3Dcomment>% Scalar =
multiplication</SPAN>
a / 4 <SPAN class=3Dcomment>% Scalar =
division</SPAN>
b =3D [5 6 7 8]'; <SPAN class=3Dcomment>% Another column =
vector</SPAN>
a + b <SPAN class=3Dcomment>% Vector =
addition</SPAN>
a - b <SPAN class=3Dcomment>% Vector =
subtraction</SPAN>
a .^ 2 <SPAN class=3Dcomment>% Element-wise =
squaring (note the ".")</SPAN>
a .* b <SPAN class=3Dcomment>% Element-wise =
multiplication (note the ".")</SPAN>
a ./ b <SPAN class=3Dcomment>% Element-wise =
division (note the ".")</SPAN>
log([1 2 3 4]) <SPAN class=3Dcomment>% Element-wise =
logarithm</SPAN>
round([1.5 2; 2.2 3.1]) <SPAN class=3Dcomment>% Element-wise =
rounding to nearest integer</SPAN>
<SPAN class=3Dcomment>% Other element-wise arithmetic operations include =
e.g. :</SPAN>
<SPAN class=3Dcomment>% floor, ceil, ...</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>% (B) Vector Operations</SPAN>
<SPAN class=3Dcomment>% Built-in Matlab functions that operate on =
vectors</SPAN>
a =3D [1 4 6 3] <SPAN class=3Dcomment>% A row =
vector</SPAN>
sum(a) <SPAN class=3Dcomment>% Sum of vector =
elements</SPAN>
mean(a) <SPAN class=3Dcomment>% Mean of vector =
elements</SPAN>
var(a) <SPAN class=3Dcomment>% Variance of =
elements</SPAN>
std(a) <SPAN class=3Dcomment>% Standard =
deviation</SPAN>
max(a) <SPAN class=3Dcomment>% Maximum</SPAN>
min(a) <SPAN class=3Dcomment>% Minimum</SPAN>
<SPAN class=3Dcomment>% If a matrix is given, then these functions will =
operate on each column</SPAN>
<SPAN class=3Dcomment>% of the matrix and return a row vector as =
result</SPAN>
a =3D [1 2 3; 4 5 6] <SPAN class=3Dcomment>% A matrix</SPAN>
mean(a) <SPAN class=3Dcomment>% Mean of each =
column</SPAN>
max(a) <SPAN class=3Dcomment>% Max of each column =
</SPAN>
max(max(a)) <SPAN class=3Dcomment>% Obtaining the max =
of a matrix </SPAN>
mean(a, 2) <SPAN class=3Dcomment>% Mean of each row =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -