MATLAB:while的用法

参考ROMS网格制作的部分代码

lonmin = 110;   % Minimum longitude [degree east]
lonmax = 130;   % Maximum longitude [degree east]
latmin =  20;   % Minimum latitude  [degree north]
latmax =  40;   % Maximum latitude  [degree north]

dl = 2;         % Grid resolution   [degree]

% Get the Longitude
lonr=(lonmin:dl:lonmax);

% Get the latitude for an isotropic grid
i=1;
latr(i)=latmin;
while latr(i)<=latmax
  i=i+1;
  latr(i)=latr(i-1)+dl*cos(latr(i-1)*pi/180);
end

latr(1)设为latmin,再后面进行“有条件的循环”,i从1到n,当lat(i)<=latmax时,计算i=i+1时的lat(i),直到计算结果>latmax时,跳出循环。

因为无法确定n具体的范围,因此用while,而不是for loop,避免n设置过大的无效循环。但需要注意的是,判断的值用的是倒数第二步的值,因此最后一位可能会>latmax

>> lonr

lonr =

   110   112   114   116   118   120   122   124   126   128   130

>> latr

latr =

   20.0000   21.8794   23.7353   25.5662   27.3703   29.1464   30.8932   32.6094   34.2942   35.9465   37.5656   39.1509   40.7019

posted @ 2023-03-09 16:17  dan-chen  阅读(184)  评论(0编辑  收藏  举报