📄 ch4_8_1.htm
字号:
<! Made by Html Translation Ver 1.0>
<HTML>
<HEAD>
<TITLE> For 回圈 </TITLE>
</HEAD>
<BODY BACKGROUND="bg0000.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/bg0000.gif">
<FONT COLOR="#0000FF">
<H1>4.8.1 For 回圈</H1>
</FONT>
<HR>
<P>
<FONT COLOR=#FF0000>for</FONT> 回圈是用在须重复执行且执行次数有一定的算式,它的结构如下:
<P>
<FONT COLOR=#FF0000>for index = array</FONT>
<P>
<FONT COLOR=#FF0000> command A</FONT>
<P>
<FONT COLOR=#FF0000>end</FONT>
<P>
如果我们要计算一缆车离铁塔的速度 (<I>v</I>),它的速度计算方式与且铁塔的距离
(<I>d</I>)有关,假设以 10 公尺为判断值,则速度计算分为二个算式:
<P>
<IMG SRC="img00029.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img4/img00029.gif">
<P>
假设有一个阵列 <I>d</I> 为缆车到铁塔的距离,则以下的<FONT COLOR=#FF0000>for</FONT>
回圈可计算速对应的速度
<P>
<FONT COLOR=#FF0000>>> for k = 1:length(d)</FONT>
<P>
<FONT COLOR=#FF0000> if d(k) <= 10</FONT>
<P>
<FONT COLOR=#FF0000> velocity = 0.425 + 0.00175*d(k)^2;</FONT>
<P>
<FONT COLOR=#FF0000> else</FONT>
<P>
<FONT COLOR=#FF0000> velocity = 0.625 + 0.12*d - 0.00025*d(k)^2;</FONT>
<P>
<FONT COLOR=#FF0000> end</FONT>
<P>
<FONT COLOR=#FF0000> fprintf('d= %f velocity= %f\n',d(k),velocity)</FONT>
<P>
<FONT COLOR=#FF0000> end<BR>
</FONT>
<P>
另外几个例子
<P>
<FONT COLOR=#FF0000>>> for n=1:10</FONT>
<P>
<FONT COLOR=#FF0000> x(n)=sin(n*pi/10);</FONT>
<P>
<FONT COLOR=#FF0000> end</FONT>
<P>
<FONT COLOR=#FF0000>>> disp(x)<BR>
</FONT>
<P>
<FONT COLOR=#FF0000>>> for n=1:5</FONT>
<P>
<FONT COLOR=#FF0000> for m=5:-1:1</FONT>
<P>
<FONT COLOR=#FF0000> A(n,m)=n^2+m^2;</FONT>
<P>
<FONT COLOR=#FF0000> end</FONT>
<P>
<FONT COLOR=#FF0000> disp(n)</FONT>
<P>
<FONT COLOR=#FF0000> end</FONT>
<P>
<FONT COLOR=#FF0000>>> disp(A)<BR>
</FONT>
<P>
但是如果可以用阵列或是矩阵运算来取代以<TT><FONT FACE="Courier New"></FONT></TT><FONT COLOR=#FF0000>for</FONT> 回圈计算,就应采用前者因为计算速度快多了。上述的例子
可改为
<P>
<FONT COLOR=#FF0000>>> n=1:10;</FONT>
<P>
<FONT COLOR=#FF0000>>> x=sin(n*pi/10);<BR>
</FONT>
<P>
使用 <FONT COLOR=#FF0000>for</FONT> 回圈的规则如下:
<OL>
<LI>上述的 <FONT COLOR=#FF0000>for</FONT> 回圈中的指标 (index)
须为是一变数。
<LI>如果 array 代表阵列是空无一物,则回圈不会被执行,例如 <FONT COLOR=#FF0000>k=1:0</FONT>。
<LI>如果 array 代表阵列是一纯量,则回圈会被执行一次,例如 <FONT COLOR=#FF0000>k=1:1</FONT>。
<LI>如果 array 代表阵列是一向量,则回圈会被依序的执行,例如 <FONT COLOR=#FF0000>k=1:b,
b=[1 3 5]</FONT>。
<LI>如果 array 代表阵列是一矩阵,则回圈会被逐行依序的执行,例如
<FONT COLOR=#FF0000>k=1:B, B=[1 2; 3 4]</FONT>。
<LI><FONT COLOR=#FF0000>for</FONT> 完整的语法为:<TT><FONT FACE="Courier New">
</FONT></TT><FONT COLOR=#FF0000>for k = first:increment:last</FONT>,其中的
<FONT COLOR=#FF0000>first</FONT>,<FONT COLOR=#FF0000> increment</FONT>,
<FONT COLOR=#FF0000>last</FONT>分别为初始值,增量,终止值。而回圈被执行的次数由以下的算式决定:
</OL>
<P>
<TT><FONT FACE="Courier New"> </FONT></TT><FONT COLOR=#FF0000>floor((last-first)/increment)+1</FONT>
<P>
如果计算得到的值为负,则回圈不被执行。<BR><HR>
<A HREF="ch4_8.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch4_8.htm"><IMG SRC="lastpage.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/lastpage.gif" BORDER=0></A>
<A HREF="ch4_8_2.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch4_8_2.htm"><IMG SRC="nextpage-1.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/nextpage.gif" BORDER=0 HSPACE=10></A>
<A HREF="index.html" tppabs="http://166.111.167.223/computer/cai/matlabjc/index.html"><IMG SRC="outline-1.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/outline.gif" BORDER=0 HSPACE=6></A><BR>
<FONT SIZE=2 COLOR=#AA55FF> 上一页 下一页 讲义大纲 </FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -