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