📄 cs143 introduction to matlab (code).mht
字号:
there is no risk of</SPAN>
<SPAN class=3Dcomment>% conflicts with the variables in the main =
workspace. At the end of a</SPAN>
<SPAN class=3Dcomment>% function execution only the output arguments =
will be visible in the</SPAN>
<SPAN class=3Dcomment>% main workspace.</SPAN>
=20
a =3D [1 2 3 4]; <SPAN class=3Dcomment>% Global variable =
a</SPAN>
b =3D myfunction(2 * a) <SPAN class=3Dcomment>% Call myfunction =
which has local </SPAN>
<SPAN class=3Dcomment>% variable a</SPAN>
a <SPAN class=3Dcomment>% Global variable a =
is unchanged</SPAN>
[c, d] =3D <SPAN class=3Dcont>...</SPAN>
myotherfunction(a, b) <SPAN class=3Dcomment>% Call =
myotherfunction with two return</SPAN>
<SPAN class=3Dcomment>% values</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>%(7) Plotting </SPAN>
x =3D [0 1 2 3 4]; <SPAN class=3Dcomment>% Basic =
plotting</SPAN>
plot(x); <SPAN class=3Dcomment>% Plot x versus its =
index values</SPAN>
pause <SPAN class=3Dcomment>% Wait for key =
press</SPAN>
plot(x, 2*x); <SPAN class=3Dcomment>% Plot 2*x versus =
x</SPAN>
axis([0 8 0 8]); <SPAN class=3Dcomment>% Adjust visible =
rectangle</SPAN>
figure; <SPAN class=3Dcomment>% Open new =
figure</SPAN>
x =3D pi*[-24:24]/24;
plot(x, sin(x));
xlabel(<SPAN class=3Dstring>'radians'</SPAN>); <SPAN =
class=3Dcomment>% Assign label for x-axis</SPAN>
ylabel(<SPAN class=3Dstring>'sin value'</SPAN>); <SPAN =
class=3Dcomment>% Assign label for y-axis</SPAN>
title(<SPAN class=3Dstring>'dummy'</SPAN>); <SPAN =
class=3Dcomment>% Assign plot title</SPAN>
figure; =20
subplot(1, 2, 1); <SPAN class=3Dcomment>% Multiple functions =
in separate graphs</SPAN>
plot(x, sin(x)); <SPAN class=3Dcomment>% (see "help =
subplot")</SPAN>
axis square; <SPAN class=3Dcomment>% Make visible area =
square</SPAN>
subplot(1, 2, 2);
plot(x, 2*cos(x));
axis square;
figure; =20
plot(x, sin(x));
hold on; <SPAN class=3Dcomment>% Multiple functions =
in single graph </SPAN>
plot(x, 2*cos(x), <SPAN class=3Dstring>'--'</SPAN>); <SPAN =
class=3Dcomment>% '--' chooses different line pattern</SPAN>
legend(<SPAN class=3Dstring>'sin'</SPAN>, <SPAN =
class=3Dstring>'cos'</SPAN>); <SPAN class=3Dcomment>% Assigns =
names to each plot</SPAN>
hold off; <SPAN class=3Dcomment>% Stop putting =
multiple figures in current</SPAN>
<SPAN class=3Dcomment>% graph</SPAN>
figure; <SPAN class=3Dcomment>% Matrices vs. =
images</SPAN>
m =3D rand(64,64);
imagesc(m) <SPAN class=3Dcomment>% Plot matrix as =
image</SPAN>
colormap gray; <SPAN class=3Dcomment>% Choose gray level =
colormap</SPAN>
axis image; <SPAN class=3Dcomment>% Show pixel =
coordinates as axes</SPAN>
axis off; <SPAN class=3Dcomment>% Remove axes</SPAN>
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
<SPAN class=3Dcomment>%(8) Working with (gray level) images</SPAN>
I =3D imread(<SPAN class=3Dstring>'cit.png'</SPAN>); <SPAN =
class=3Dcomment>% Read a PNG image</SPAN>
figure
imagesc(I) <SPAN class=3Dcomment>% Display it as gray =
level image</SPAN>
colormap gray;
colorbar <SPAN class=3Dcomment>% Turn on color bar =
on the side</SPAN>
pixval <SPAN class=3Dcomment>% Display pixel =
values interactively</SPAN>
truesize <SPAN class=3Dcomment>% Display at =
resolution of one screen</SPAN>
<SPAN class=3Dcomment>% pixel per image =
pixel</SPAN>
truesize(2*size(I)) <SPAN class=3Dcomment>% Display at =
resolution of two screen</SPAN>
<SPAN class=3Dcomment>% pixels per image =
pixel</SPAN>
I2 =3D imresize(I, 0.5, <SPAN class=3Dstring>'bil'</SPAN>); <SPAN =
class=3Dcomment>% Resize to 50% using bilinear </SPAN>
<SPAN class=3Dcomment>% =
interpolation</SPAN>
I3 =3D imrotate(I2, 45, <SPAN class=3Dcont>...</SPAN><SPAN =
class=3Dcomment> % Rotate 45 degrees and crop to</SPAN>
<SPAN class=3Dstring>'bil'</SPAN>, <SPAN =
class=3Dstring>'crop'</SPAN>); <SPAN class=3Dcomment>% original =
size</SPAN>
I3 =3D double(I2); <SPAN class=3Dcomment>% Convert from =
uint8 to double, to allow</SPAN>
<SPAN class=3Dcomment>% math =
operations</SPAN>
imagesc(I3.^2) <SPAN class=3Dcomment>% Display squared =
image (pixel-wise)</SPAN>
imagesc(log(I3)) <SPAN class=3Dcomment>% Display log of =
image (pixel-wise)</SPAN>
I3 =3D uint8(I3); <SPAN class=3Dcomment>% Convert back to =
uint8 for writing</SPAN>
imwrite(I3, <SPAN class=3Dstring>'test.png'</SPAN>) <SPAN =
class=3Dcomment>% Save image as PNG</SPAN>
figure;
g =3D [1 2 1]' * [1 2 1] / 16; <SPAN class=3Dcomment>% 3x3 Gaussian =
filter mask</SPAN>
I2 =3D double(I); <SPAN class=3Dcomment>% Convert image to =
floating point</SPAN>
I3 =3D conv2(I2, g); <SPAN class=3Dcomment>% Convolve image =
with filter mask</SPAN>
I3 =3D conv2(I2, g, <SPAN class=3Dstring>'same'</SPAN>); <SPAN =
class=3Dcomment>% Convolve image, but keep original size</SPAN>
subplot(1, 2, 1) <SPAN class=3Dcomment>% Display original =
and filtered image</SPAN>
imagesc(I); <SPAN class=3Dcomment>% =
side-by-side</SPAN>
axis square;
colormap gray;
subplot(1, 2, 2)
imagesc(I3);
axis square;
colormap gray;
<SPAN =
class=3Dcomment>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
%%%%%%%%%%%%%%</SPAN>
</PRE>
<H2>myfunction.m</H2><A id=3Dmyfunction name=3Dmyfunction></A><PRE =
class=3Dmcode><SPAN class=3Dkeyword>function</SPAN> y =3D myfunction(x)
<SPAN class=3Dcomment>% Function of one argument with one return =
value</SPAN>
a =3D [-2 -1 0 1]; <SPAN class=3Dcomment>% Have a global =
variable of the same name</SPAN>
y =3D a + x;
</PRE>
<H2>myotherfunction.m</H2><A id=3Dmyotherfunction =
name=3Dmyotherfunction></A><PRE class=3Dmcode><SPAN =
class=3Dkeyword>function</SPAN> [y, z] =3D myotherfunction(a, b)
<SPAN class=3Dcomment>% Function of two arguments with two return =
values</SPAN>
y =3D a + b;
z =3D a - b;
</PRE>
<HR>
<DIV class=3Dnavfoot>
<DIV class=3Dfootname>Created by <A=20
href=3D"mailto:%20roth%20(AT)%20cs%20DOT%20brown%20DOT%20edu">Stefan =
Roth</A>=20
</DIV>
<DIV class=3Dfootdate>Last updated: 2003-09-10 =
</DIV></DIV></BODY></HTML>
------=_NextPart_000_0000_01C9D2E5.82990560
Content-Type: text/css;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.cs.brown.edu/courses/cs143/plain.css
BODY {
PADDING-RIGHT: 5%; PADDING-LEFT: 5%; BACKGROUND: #fff; PADDING-BOTTOM: =
5%; MARGIN: 0px; FONT: 12pt/1.2 Arial, Helvetica, Verdana, sans-serif; =
PADDING-TOP: 2%
}
A {
FONT-WEIGHT: bold; COLOR: #00a; TEXT-DECORATION: none
}
A:hover {
BACKGROUND: #00a; COLOR: #fff
}
H1 A {
COLOR: #666
}
H2 A {
COLOR: #666
}
H3 A {
COLOR: #666
}
H4 A {
COLOR: #666
}
H1 {
PADDING-RIGHT: 2px; PADDING-LEFT: 2px; FONT-SIZE: 20pt; PADDING-BOTTOM: =
2px; MARGIN: 0px 0px 8px; COLOR: #666; PADDING-TOP: 2px; BORDER-BOTTOM: =
#000 5px solid
}
H2 {
PADDING-RIGHT: 2px; MARGIN-TOP: 5px; PADDING-LEFT: 2px; FONT-SIZE: =
16pt; PADDING-BOTTOM: 2px; COLOR: #666; PADDING-TOP: 2px; BORDER-BOTTOM: =
#000000 2px solid
}
H3 {
PADDING-RIGHT: 2px; MARGIN-TOP: 5px; PADDING-LEFT: 2px; FONT-SIZE: =
14pt; PADDING-BOTTOM: 2px; COLOR: #666; PADDING-TOP: 2px; BORDER-BOTTOM: =
#000 1px dashed
}
H4 {
PADDING-RIGHT: 2px; MARGIN-TOP: 5px; PADDING-LEFT: 2px; FONT-SIZE: =
14pt; PADDING-BOTTOM: 2px; COLOR: #666; PADDING-TOP: 2px
}
IMG {
PADDING-RIGHT: 2px; PADDING-LEFT: 2px; FLOAT: right; PADDING-BOTTOM: =
2px; MARGIN: 10px; PADDING-TOP: 2px
}
#im {
CLEAR: right
}
PRE {
BORDER-RIGHT: #777777 1px solid; PADDING-RIGHT: 0.5em; BORDER-TOP: =
#777777 1px solid; PADDING-LEFT: 0.5em; PADDING-BOTTOM: 0.5em; =
MARGIN-LEFT: 1em; BORDER-LEFT: #777777 1px solid; COLOR: black; =
MARGIN-RIGHT: 2em; PADDING-TOP: 0.5em; BORDER-BOTTOM: #777777 1px solid; =
WHITE-SPACE: pre; BACKGROUND-COLOR: #e6e6e6
}
.verse {
MARGIN-LEFT: 1em; WHITE-SPACE: pre
}
DT {
FONT-WEIGHT: bold
}
TD {
FONT: 12pt/1.2 Arial, Helvetica, Verdana, sans-serif
}
.menu {
FONT-SIZE: 16pt; TEXT-ALIGN: center
}
.submenu {
TEXT-ALIGN: center
}
.navfoot .footname {
FLOAT: left; WIDTH: 49%; TEXT-ALIGN: left
}
.navfoot .footdate {
FLOAT: right; WIDTH: 49%; TEXT-ALIGN: right
}
.mcode .comment {
COLOR: #228b22
}
.mcode .string {
COLOR: #b20000
}
.mcode .keyword {
COLOR: #0000ff
}
.mcode .cont {
COLOR: #0000ff
}
.mcode .cont {
TEXT-DECORATION: underline
}
.mcode .code {
COLOR: #000000
}
------=_NextPart_000_0000_01C9D2E5.82990560--
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -