一个简单求和函数的matlab实现(带程序耗时功能)
来源:互联网 发布:淘宝怎么做虚拟产品 编辑:程序博客网 时间:2024/06/05 18:18
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:28tocend
</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%用来计时的,没什么作用,纯粹是为了跟大家讲解这么个用法%{sdfdsfsdfdsfdsf%}%利用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:28tocend
0 0
- 一个简单求和函数的matlab实现(带程序耗时功能)
- 计算程序耗时的简单实现代码
- MATLAB实现一个简单的车牌识别小程序
- 实现一个简单的摄像功能(不带传输数据)代码片段
- 用Python实现一个1加到n求和功能的函数
- 一个简单的BP神经网络matlab程序(附函数详解)
- 一个简单求和函数的C-》SSE-》AVX的实现过程
- 一个简单的两数求和程序的小小改动
- 一个简单的matlab图形界面程序GUI
- 一个简单的傅里叶变换matlab函数(绝对可用)
- java实现一个简单的HTTP服务器,带打开网页和计算功能
- 【matlab】一个简单的matlab图形界面程序GUI
- matlab实现简单的交互式程序
- 用Python实现一个简单的能够发送带附件的邮件程序的教程
- 一个简单的js求和
- 一个简单的数列求和
- 一个带命令行解析功能的console程序框架
- 一个带命令行解析功能的console程序框架
- Android中的线程
- 第十三周项目-项目3-形状类族的纯虚函数
- 最大子数组和
- 解决KeSetTimer定时精度不够方法
- JAVA基础之运算符
- 一个简单求和函数的matlab实现(带程序耗时功能)
- Powershell中禁止执行脚本解决办法(about_Execution_Policies)
- 欢迎使用CSDN-markdown编辑器
- promise应用示例讲解
- JAVA入门 新手看类与对象
- Structs2 命名空间
- JAVA基础之程序流程控制:判断结构
- TCP/IP详解(五)---IP选路
- 模式识别(Pattern Recognition)学习笔记(十一)--SVM支持向量机(狭义):大间隔