文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 高等教育 > 工学 > Stephen J.Chapman《MATLAB 编程(第二版) 》第四章
侵权投诉

Stephen J.Chapman《MATLAB 编程(第二版) 》第四章

Stephen J.Chapman《MATLAB 编程(第二版) 》第四章

用两种不同的方法计算出大于5000的元素的平方根。

比较两种方法运行速度的matlab程序如下所示: % Script file:logical1.m %

% Purpose:

% This program calculates the time required to % calculate the square roots of all elements in % array a whose value exceeds 5000. This is done % in two differents ways:

% 1.Using a for loop and if construct. % 2.Using a logical array. %

% Record of revisions:

% Date Programmer Description of change

% ==== ================ ============================== % 06/01/02 S. J. Chapman Original code %

% Define variables:

% a --Array of input values

% b --Logical array to serve as a mask % ii,jj --Loop index % average1 --Average time for calculation 1 % average2 --Average time for calculation 2 % maxcount --Number of times to loop calculation % month --Month (mm) % year --Year (yyyy) %

% Perform calculation using loops and branches maxcount = 1; % One repetition tic; % Start timer for jj = 1:maxcount a = 1:10000; %Declare array a for ii = 1:10000 if a(ii) > 5000 a(ii) = sqrt(a(ii)); end end end

average1 = (toc)/maxcount;%Calculate average time %

% Perform calculation using logical arrays. maxcount = 10; %One repetition tic; %Start timer for jj = 1:maxcount a = 1:10000; %Declare array a b = a > 5000; %Create mask a(b) = sqrt(a(b)); %Take square root end

average2 = (toc)/maxcount; %Calculate average time %

% Display result

fprintf('Loop /if approach = %8.4f\n',average1);

fprintf('Logical array approach = %8.4f\n',average2);

这个程序在cpu为奔腾III(主频为733mhz)的计算机运行得到结果如下 >> logical1

Loop /if approach = 0.1200

上一页第17页

你可能喜欢

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email