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
Stephen J.Chapman《MATLAB 编程(第二版) 》第一章_工学_高等教育_教育专区。Stephen J.Chapman《MATLAB 编程(第二版) 》郑碧波翻译 Matlab 中文论坛首发 http:...
Stephen J.Chapman《MATLAB 编程(第二版) 》第二章Stephen J.Chapman《MATLAB 编程(第二版) 》第二章隐藏>> 郑碧波翻译 Matlab 中文论坛首发 http://www.iLo...
Stephen J.Chapman《MATLAB 编程(第二版) 》第三章Stephen J.Chapman《MATLAB 编程(第二版) 》第三章隐藏>> 郑碧波翻译 Matlab 中文论坛首发 http://www.iLove...
Stephen J.Chapman《MATLAB 编程(第二版) 》第六章Stephen J.Chapman《MATLAB 编程(第二版) 》第六章隐藏>> 郑碧波翻译 Matlab 中文论坛首发 http://www.iLo...
Matlab编程 第四版(Stephen J.Chapman)Quiz部分答案_计算机软件及应用_IT/计算机_专业资料。Matlab编程 第四版 (Stephen J.ChapmanQuiz答案PD...
郑碧波翻译 Matlab 中文论坛首发 http://www.iLoveMatlab.cn 前言 本文为 Stephen J.Chapman《MATLAB 编程(第二版) 》英文影印版的中文译本,这本 书对初学者很...
Chapman 著 邢树军 郑碧波 译 前言郑碧波本文为 Stephen J.Chapman《MATLAB 编程(第二版) 》英文影印版的中文译本,这本 书对初学者很好的入门教材。之前从网...
matlab编程英文版(Stephen J. Chapman )课后答案1_工学_高等教育_教育专区。matlab编程第二版Stephen J. ChapmanMATLAB MATLAB Programming for Enginees Enginees 姓名...
S.J.Chapman《MATLAB编程》中文版修正版_部分7_信息与通信_工程科技_专业资料。...>> cellstring{1} = 'Stephen J. Chapman'; >> cellstring{2} = 'Male'...
S.J.Chapman《MATLAB编程》中文版修正版_部分3_信息与通信_工程科技_专业资料。...Stephen J.Chapman《MAT... 39页 免费 Stephen J.Chapman《MAT... 37页 免...
热门文档