一个简单求和函数的matlab实现(带程序耗时功能)

function [ sum_m ] = diffSum( )
%function [ sum_m ] = diffSum(beginning ,ending,dim)
tic%用来计时的,没什么作用,纯粹是为了跟大家讲解这么个用法
%利用while,for等语言实现简单求和运算,作者:nicolashe,2016年5月9日 11:13:26
%   begin代码起始数,end代表终止数,都是闭口的,dim表示选择用的模式1表示
%表示采用while,2表示采用for,为了展示给你们看,采用switch,也可以使用if实现模式的选择,默认采用1
%%下面利用switch实现选择
%实际上我此时已经忘记了switch的语法,但是不要紧,可以即学即用啊,我查帮助文档得知
%{
 help switch
 switch Switch among several cases based on expression.
    The general form of the switch statement is:
 
        switch switch_expr
          CASE case_expr, 
            statement, ..., statement
          CASE {case_expr1, case_expr2, case_expr3,...}
            statement, ..., statement
         ...
          OTHERWISE, 
            statement, ..., statement
        END
 
    The statements following the first CASE where the switch_expr matches
    the case_expr are executed.  When the case expression is a cell array
    (as in the second case above), the case_expr matches if any of the
    elements of the cell array match the switch expression.  If none of
    the case expressions match the switch expression then the OTHERWISE
    case is executed (if it exists).  Only one CASE is executed and
    execution resumes with the statement after the END.
 
    The switch_expr can be a scalar or a string.  A scalar switch_expr
    matches a case_expr if switch_expr==case_expr.  A string
    switch_expr matches a case_expr if strcmp(switch_expr,case_expr)
    returns 1 (true).
 
    Only the statements between the matching CASE and the next CASE,
    OTHERWISE, or END are executed.  Unlike C, the switch statement
    does not fall through (so BREAKs are unnecessary).
 
    Example:
 
    To execute a certain block of code based on what the string, METHOD, 
    is set to,
 
        method = 'Bilinear';
 
        switch lower(method)
          case {'linear','bilinear'}
            disp('Method is linear')
          case 'cubic'
            disp('Method is cubic')
          case 'nearest'
            disp('Method is nearest')
          otherwise
            disp('Unknown method.')
        end
 
        Method is linear
 
    See also case, otherwise, if, while, for, end.

    Reference page in Help browser
       doc switch

%}
%提示输入相关数据
beginning=input('请您输入你要求和的起始数:');
ending=input('请您输入要求和的终止数:');

dim=input('请您输入相关模式,1表示使用while,2表示使用for来实现,dim= ');
%%
% if isempty(dim)
%     dim=0;%默认它一个数,否则您没有输入的话,switch会出错;
% else
%     dim=dim;
%%
sum_m=zeros(1);%预置一个数来承装最终的和
 temp=zeros(1);%预置一个数来作为临时存数器       
  switch dim
          case 1
         t1=clock;
                  temp=beginning;
         while (temp<=ending)
             sum_m=sum_m+temp;
             temp=temp+1;
                      
         end
           disp(['while程序总运行时间:',num2str(etime(clock,t1))]);
              case 2
         t1=clock;
         for temp=beginning:ending
             sum_m=sum_m+temp;
           
         end
           disp(['for程序总运行时间:',num2str(etime(clock,t1))]);
              otherwise
         t1=clock;
         sum_m=sum((beginning:ending));
         disp(['sum程序总运行时间:',num2str(etime(clock,t1))]);
 end
 
        %%
disp(['起始数:',num2str(beginning),'终止数:',num2str(ending),'的和是:',num2str(sum_m)]);
%程序写完了2016年5月9日 11:34:28
toc
end
</pre><pre code_snippet_id="1692297" snippet_file_name="blog_20160522_3_2089978" name="code" class="plain">计时:
</pre><p></p><pre code_snippet_id="1692297" snippet_file_name="blog_20160522_5_8393243" name="code" class="plain">function [ sum_m ] = diffSum( )
%function [ sum_m ] = diffSum(beginning ,ending,dim)
tic%用来计时的,没什么作用,纯粹是为了跟大家讲解这么个用法
%{
sdfdsf
sdfdsfdsf
%}


%利用while,for等语言实现简单求和运算,作者:nicolashe,2016年5月9日 11:13:26
%   begin代码起始数,end代表终止数,都是闭口的,dim表示选择用的模式1表示
%表示采用while,2表示采用for,为了展示给你们看,采用switch,也可以使用if实现模式的选择,默认采用1
%%下面利用switch实现选择
%实际上我此时已经忘记了switch的语法,但是不要紧,可以即学即用啊,我查帮助文档得知
%{
 help switch
 switch Switch among several cases based on expression.
    The general form of the switch statement is:
 
        switch switch_expr
          CASE case_expr, 
            statement, ..., statement
          CASE {case_expr1, case_expr2, case_expr3,...}
            statement, ..., statement
         ...
          OTHERWISE, 
            statement, ..., statement
        END
 
    The statements following the first CASE where the switch_expr matches
    the case_expr are executed.  When the case expression is a cell array
    (as in the second case above), the case_expr matches if any of the
    elements of the cell array match the switch expression.  If none of
    the case expressions match the switch expression then the OTHERWISE
    case is executed (if it exists).  Only one CASE is executed and
    execution resumes with the statement after the END.
 
    The switch_expr can be a scalar or a string.  A scalar switch_expr
    matches a case_expr if switch_expr==case_expr.  A string
    switch_expr matches a case_expr if strcmp(switch_expr,case_expr)
    returns 1 (true).
 
    Only the statements between the matching CASE and the next CASE,
    OTHERWISE, or END are executed.  Unlike C, the switch statement
    does not fall through (so BREAKs are unnecessary).
 
    Example:
 
    To execute a certain block of code based on what the string, METHOD, 
    is set to,
 
        method = 'Bilinear';
 
        switch lower(method)
          case {'linear','bilinear'}
            disp('Method is linear')
          case 'cubic'
            disp('Method is cubic')
          case 'nearest'
            disp('Method is nearest')
          otherwise
            disp('Unknown method.')
        end
 
        Method is linear
 
    See also case, otherwise, if, while, for, end.

    Reference page in Help browser
       doc switch

%}
%提示输入相关数据
beginning=input('请您输入你要求和的起始数:');
ending=input('请您输入要求和的终止数:');

dim=input('请您输入相关模式,1表示使用while,2表示使用for来实现,dim= ');
%%
% if isempty(dim)
%     dim=0;%默认它一个数,否则您没有输入的话,switch会出错;
% else
%     dim=dim;
%%
sum_m=zeros(1);%预置一个数来承装最终的和
 temp=zeros(1);%预置一个数来作为临时存数器       
  switch dim
          case 1
         t1=clock;
                  temp=beginning;
         while (temp<=ending)
             sum_m=sum_m+temp;
             temp=temp+1;
                      
         end
           disp(['while程序总运行时间:',num2str(etime(clock,t1))]);
              case 2
         t1=clock;
         for temp=beginning:ending
             sum_m=sum_m+temp;
           
         end
           disp(['for程序总运行时间:',num2str(etime(clock,t1))]);
              otherwise
         t1=clock;
         sum_m=sum((beginning:ending));
         disp(['sum程序总运行时间:',num2str(etime(clock,t1))]);
 end
 
        %%
disp(['起始数:',num2str(beginning),'终止数:',num2str(ending),'的和是:',num2str(sum_m)]);
%程序写完了2016年5月9日 11:34:28
toc
end


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值